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.

Signature
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Chris Devers - 28 Dec 2005 18:46 GMT
> Why the a.pl is faster than b.pl? I think ever the resulte should be
> opposite.Thanks.
The easiest way to answer such questions is to benchmark and profile
where the time in each script is being spent.
These two scripts are so different in composition that it isn't
immediately obvious to me how they're similar or dis-similar.
You have two approaches you can try for answering such questions:
* have two nearly identical scripts, and measure how the small
different part impacts performance.
* break each script into components and measure how long each
component takes to complete its task.
These approaches can be intermixed as needed, but it's up to you to do
the fundamental measuring of your code for yourself.
Distill the question down to something clearer -- "why is statement (or
subroutine) A faster than statement B while having the same result" --
and you may find more concrete advice from the list members.

Signature
Chris Devers
DO NOT LEAVE IT IS NOT REAL
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Bob Showalter - 28 Dec 2005 18:54 GMT
> hi,lists,
>
[quoted text clipped - 65 lines]
>
> 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.

Signature
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jeff Pang - 29 Dec 2005 01:22 GMT
Hi,bob,
You said:
3. It will probably be faster to use a single regex of the format:
/pata|patb|patc|patd/
In fact maybe you are wrong on this.Based on my test case,the RE written as below:
/pata/ || /patb/ || /patc/ || /patd/
is much faster than yours.
-----Original Message-----
>From: Bob Showalter <bob_showalter@taylorwhite.com>
>Sent: Dec 29, 2005 2:54 AM
[quoted text clipped - 95 lines]
>If the alternation can stay inside the regex code rather than happening
> out at the Perl opcode level, it might be faster.

Signature
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>