Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

[perl #32331] grep {/PATTERN/} is slow

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


perlbug-followup at perl

Apr 29, 2012, 6:49 PM

Post #1 of 2 (77 views)
Permalink
[perl #32331] grep {/PATTERN/} is slow

On Fri Dec 03 11:22:39 2004, ajs wrote:
> Sorry, I submitted before I was done. My final version of the test:
>
> use Benchmark qw /cmpthese/;
>
> our @array = 101 .. 20000;
> our $b1;
> our $b2;
>
> cmpthese -2 => {
> grep_re_expr => '$::b1 = grep /50/, @::array',
> grep_re_blck => '$::b1 = grep {/50/} @::array',
> grep_re_subr => '$::b2 = grep {is50($_)} @::array',
> };
>
> die unless $b1 == $b2;
>
> sub is50 {
> return ($_[0] =~ /50/);
> }
>
> I also tried:
>
> our $p = qr{^50$};
>
> and then something like:
>
> grep_re_blck => '$::b1 = grep {/$p/} @::array',
>
> which was very slightly faster (relative to the expr version with the
> pre-compiled regexp), so this makes me suspect repeated re-parsing of
> the regexp.


This is still present in blead; But as far as I can tell, the slowdown
is because the block version needs to create and enter/leave a scope,
while the expression version has no such penalty; So unless I'm wrong, I
think this has to be marked as WontFix.


---
via perlbug: queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=32331


perlbug-followup at perl

Apr 30, 2012, 5:53 AM

Post #2 of 2 (67 views)
Permalink
[perl #32331] grep {/PATTERN/} is slow [In reply to]

On Sun Apr 29 18:49:32 2012, Hugmeir wrote:
> On Fri Dec 03 11:22:39 2004, ajs wrote:
> > Sorry, I submitted before I was done. My final version of the test:
> >
> > use Benchmark qw /cmpthese/;
> >
> > our @array = 101 .. 20000;
> > our $b1;
> > our $b2;
> >
> > cmpthese -2 => {
> > grep_re_expr => '$::b1 = grep /50/, @::array',
> > grep_re_blck => '$::b1 = grep {/50/} @::array',
> > grep_re_subr => '$::b2 = grep {is50($_)} @::array',
> > };
> >
> > die unless $b1 == $b2;
> >
> > sub is50 {
> > return ($_[0] =~ /50/);
> > }
> >
> > I also tried:
> >
> > our $p = qr{^50$};
> >
> > and then something like:
> >
> > grep_re_blck => '$::b1 = grep {/$p/} @::array',
> >
> > which was very slightly faster (relative to the expr version with the
> > pre-compiled regexp), so this makes me suspect repeated re-parsing of
> > the regexp.
>
>
> This is still present in blead; But as far as I can tell, the slowdown
> is because the block version needs to create and enter/leave a scope,
> while the expression version has no such penalty; So unless I'm wrong, I
> think this has to be marked as WontFix.

But the enter/leave shouldn’t make this much difference:

The first script, which uses $_==50:

$ pbpaste|perl5.15.9
Rate grep_eq_blck grep_eq_expr
grep_eq_blck 42400/s -- -4%
grep_eq_expr 44245/s 4% --

The second script, which uses /^50$/:

$ pbpaste|perl5.15.9
Rate grep_re_blck grep_re_expr
grep_re_blck 9481/s -- -69%
grep_re_expr 30488/s 222% --

--

Father Chrysostomos


---
via perlbug: queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=32331

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.