Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Getting Started / December 2005



Tip: Looking for answers? Try searching our database.

why a.pl is faster than b.pl

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff Pang - 28 Dec 2005 12:38 GMT
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>

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.