why a.pl is faster than b.pl



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.
.



Relevant Pages

  • Scope
    ... simple scope question ... the "my" on $service was required by strict) ... This is perl, ... AND d03 = ?") or die; ...
    (perl.beginners)
  • Re: why die; produce an error?
    ... > use strict; ... > use warnings; ... but when I put die; at the end of script it ... I'm running it using "perl test.pl". ...
    (perl.beginners)
  • Re: Help optimizing script
    ... It looks nice Jeff, you die, use strict and warnings - very very very ... good way to begin perl! ...
    (perl.beginners)
  • Linux Magazin: Perl gewinnt haushoch gegen die anderen Skriptsprachen
    ... [@Randal, in case your German's not so good: I was dismayed how they turned this contest around, to make it seem at first sight like you and Perl had completely failed. ... Klar ist es blöd, daß alle nach dem Motto programmieren, CPU und Speicher seien eh da. ... Da ich bei makepp viel Erfahrung mit Perl Optimierung gesammelt habe, bin ich da gerne ran gegangen. ... Randal hat achtlos die drei Infos pro Fußnote in einen Hash gespeichert. ...
    (comp.lang.perl.misc)
  • Re: When to "use strict" when teaching?
    ... >>being influenced by the history of Perl ... history bias you" essentially the same as saying you are not ... An understanding of how symbolic references work is neither necessary ... I say don't expose people to the idea of omitting "use strict" until ...
    (comp.lang.perl.misc)