
perlbug-followup at perl
Nov 4, 2004, 3:55 PM
Post #1 of 6
(166 views)
Permalink
|
|
[perl #32331] grep {/PATTERN/} is slow
|
|
# New Ticket Created by abigail [at] abigail # Please include the string: [perl #32331] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org:80/rt3/Ticket/Display.html?id=32331 > This is a bug report for perl from abigail [at] abigail, generated with the help of perlbug 1.35 running under perl v5.8.5. ----------------------------------------------------------------- [Please enter your report here] I would expect grep {EXPR} LIST to be somewhat slower than grep EXPR, LIST For instance, the following benchmark shows the block version to be 5% slower: use Benchmark qw /cmpthese/; our @array = 101 .. 200; cmpthese -2 => { grep_eq_expr => '$::a1 = grep $_ == 50, @::array', grep_eq_blck => '$::a2 = grep {$_ == 50} @::array', }; __END__ Rate grep_eq_blck grep_eq_expr grep_eq_blck 28902/s -- -5% grep_eq_expr 30282/s 5% -- If, however, the expression is a regexp, things change drastically: use Benchmark qw /cmpthese/; our @array = 101 .. 200; cmpthese -2 => { grep_re_expr => '$::b1 = grep /^50$/, @::array', grep_re_blck => '$::b2 = grep {/^50$/} @::array', }; __END__ Rate grep_re_blck grep_re_expr grep_re_blck 6455/s -- -69% grep_re_expr 20848/s 223% -- This difference in speed surprises me. It's not the fact that it's a regexp that's run inside a block, as a similar for loop doesn't show the huge difference: use Benchmark qw /cmpthese/; our @array = 101 .. 200; cmpthese -2 => { for_eq_expr => '$::c1 = 0; $_ == 50 and $::c1 ++ for @::array', for_eq_blck => '$::c2 = 0; for (@::array) {$_ == 50 and $::c2 ++}', }; __END__ Rate for_eq_blck for_eq_expr for_eq_blck 23640/s -- -5% for_eq_expr 24992/s 6% -- use Benchmark qw /cmpthese/; our @array = 101 .. 200; cmpthese -2 => { for_re_expr => '$::d1 = 0; /^50$/ and $::d1 ++ for @::array', for_re_blck => '$::d2 = 0; for (@::array) {/^50$/ and $::d2 ++}', }; __END__ Rate for_re_blck for_re_expr for_re_blck 19091/s -- -2% for_re_expr 19545/s 2% -- In both cases, the block version is slower, but by a small margin. So, I suspect a bug somewhere. I'll be leaving for my honeymoon and YAPC::AU tomorrow morning, so it might be a while before I'll be able to reply to any questions. Abigail [Please do not change anything below this line] ----------------------------------------------------------------- --- Flags: category=core severity=low --- Site configuration information for perl v5.8.5: Configured by abigail at Tue Aug 3 21:00:04 CEST 2004. Summary of my perl5 (revision 5 version 8 subversion 5) configuration: Platform: osname=linux, osvers=2.4.18-bf2.4, archname=i686-linux-64int-ld uname='linux alexandra 2.4.18-bf2.4 #1 son apr 14 09:53:28 cest 2002 i686 unknown ' config_args='-des -Dusemorebits -Uversiononly -Dmydomain=.abigail.nl -Dcf_email=abigail [at] abigail -Dperladmin=abigail [at] abigail -Doptimize=-g -Dcc=gcc -Dprefix=/opt/perl' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=define use64bitall=undef uselongdouble=define usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags ='-DDEBUGGING -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64', optimize='-g', cppflags='-DDEBUGGING -fno-strict-aliasing -pipe -I/usr/local/include' ccversion='', gccversion='3.0.4', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long long', ivsize=8, nvtype='long double', nvsize=12, Off_t='off_t', lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='gcc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -ldl -lm -lcrypt -lutil -lc perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc libc=/lib/libc-2.2.5.so, so=so, useshrplib=false, libperl=libperl.a gnulibc_version='2.2.5' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E' cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib' Locally applied patches: defined-or no-syntax-warnings --- @INC for perl v5.8.5: /home/abigail/Perl /opt/perl/lib/5.8.5/i686-linux-64int-ld /opt/perl/lib/5.8.5 /opt/perl/lib/site_perl/5.8.5/i686-linux-64int-ld /opt/perl/lib/site_perl/5.8.5 /opt/perl/lib/site_perl/5.8.4/i686-linux-64int-ld /opt/perl/lib/site_perl/5.8.4 /opt/perl/lib/site_perl/5.8.3/i686-linux-64int-ld /opt/perl/lib/site_perl/5.8.3 /opt/perl/lib/site_perl/5.8.2/i686-linux-64int-ld /opt/perl/lib/site_perl/5.8.2 /opt/perl/lib/site_perl/5.8.1/i686-linux-64int-ld /opt/perl/lib/site_perl/5.8.1 /opt/perl/lib/site_perl/5.8.0/i686-linux-64int-ld /opt/perl/lib/site_perl/5.8.0 /opt/perl/lib/site_perl . --- Environment for perl v5.8.5: HOME=/home/abigail LANG=C LANGUAGE (unset) LD_LIBRARY_PATH=/home/abigail/Lib:/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib LOGDIR (unset) PATH=/home/abigail/Bin:/opt/perl/bin:/usr/local/bin:/usr/local/X11/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/games:/usr/share/texmf/bin:/opt/Acrobat/bin:/opt/java/blackdown/j2sdk1.3.1/bin:/usr/local/games/bin PERL5LIB=/home/abigail/Perl PERLDIR=/opt/perl PERL_BADLANG (unset) SHELL=/bin/bash
|