Re: why a.pl is faster than b.pl



Jeff Pang wrote:
hi,lists,

I have two perl scripts as following:

a.pl:
----
#!/usr/bin/perl
use strict;

my @logs = glob "~/logs/rcptstat/da2005_12_28/da.127.0.0.1.*";

foreach my $log (@logs) {
open (HD,$log) or die "$!";
while(<HD>){

if ( ($_ =~ /×¢²á/o) || ($_ =~ /Õ÷ÎÄ/o) || ($_ =~ /Ê¥µ®¿ìÀÖ/) || ($_ =~ /ӦƸ/o) || ($_ =~ /�ø�¨/o) || ($_ =~ /·¢»õ/o) || ($_ =~ /±±¾©/o) || ($_ =~ /×Ê��/o) || ($_ =~ /�Å�¢/o) || ($_ =~ /�ãɽ/o) || ($_ =~ /°Ù�ò/o) || ($_ =~ /Ãâ·Ñ/o) ) {
print $_;
}
}
close HD;
}



b.pl ---- #!/usr/bin/perl use strict;

  my $ref = sub { $_[0] =~ /×¢²á/o || $_[0] =~ /Õ÷ÎÄ/o || $_[0] =~ /Ê¥µ®¿ìÀÖ/o ||
                  $_[0] =~ /ӦƸ/o || $_[0] =~ /�ø�¨/o || $_[0] =~ /·¢»õ/o ||
                  $_[0] =~ /±±¾©/o || $_[0] =~ /×Ê��/o || $_[0] =~ /�Å�¢/o ||
                  $_[0] =~ /�ãɽ/o || $_[0] =~ /°Ù�ò/o || $_[0] =~ /Ãâ·Ñ/o };


my @logs = glob "~/logs/rcptstat/da2005_12_28/da.127.0.0.1.*";

foreach my $log (@logs) {
open (HD,$log) or die "$!";
while(<HD>){
    print if $ref->($_);
  }
close HD;
}


I run the 'time' command to get the running speed:

time perl a.pl > /dev/null

real    0m0.190s
user    0m0.181s
sys     0m0.008s


time perl b.pl > /dev/null


real    0m0.286s
user    0m0.278s
sys     0m0.007s


Why the a.pl is faster than b.pl? I think ever the resulte should be opposite.Thanks.


Well, the time differences aren't dramatic. But off hand, I would say that a.pl is faster because no subroutine call is involved.


A couple of other observations:

1. /o is useless on these regexes, since they don't interpolate any variables.

2. $_ is the default target for the m// operator, so

   $_ =~ /regex/

can be replaced with simply

   /regex/

3. It will probably be faster to use a single regex of the format:

   /pata|patb|patc|patd/

If the alternation can stay inside the regex code rather than happening out at the Perl opcode level, it might be faster.
.




Relevant Pages

  • Re: quetion about "+=" and "runtime"
    ... user 0m0.030s sys 0m0.010s ... Are the graves of dreams allowed to die." ...
    (comp.lang.python)
  • Re: Begniner Question
    ... > import sys ... Robert Kern ... Are the graves of dreams allowed to die." ...
    (comp.lang.python)
  • Re: printing die error ina file
    ... Jeff Pang wrote: ... > and 'warn' handler and wrote the results to files. ... NetEase AntiSpam Team ... Many other Perl's errors, like division by zero, are not handle via die or warn. ...
    (perl.beginners)
  • Re: printing die error ina file
    ... Jeff Pang wrote: ... other Perl's errors, like division by zero, are not handle via die or warn. ... If you have a real hardware problem, your program might be the first to find it but you will have programs failing all over the place. ...
    (perl.beginners)
  • Re: merging the columns
    ... Use of uninitialized value in scalar chomp at xxx.pl line 12. ... $ cat 1.txt aaa ... open my $fh1,'1.txt' or die $!; ... Jeff Pang - pangj@xxxxxxxxxxxxx ...
    (perl.beginners)