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



Jeff Pang wrote:
> 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.

Can you explain why you believe b.pl should be faster? From looking at
your code, I see that b.pl has to do the following extra work:
1) Dereference a subroutine reference for every iteration of the while
loop
2) Call a subroutine for every iteration of the while loop
3) Retrieve the first element of @_ for every pattern match for every
iteration of the while loop (retrieving an element from an array is
more work than retrieving a simple scalar)

If you're really curious about what pieces of your code take what
amounts of time, allow me to suggest looking into the Devel::DProf
module, which is a core module:

perldoc Devel::DProf

Paul Lalli

.



Relevant Pages

  • Re: $lineNumber +=1 does not increment
    ... > I am trying to parse a file with the following code: ... > use strict; ... iteration of your foreach loop, ...
    (comp.lang.perl.misc)
  • Re: File handle re-use woes using pipe within a loop
    ... >> I'd recommend using strict and lexical file handles, ... If something is lexically scoped within a loop then each time through ... This uses sybolic handle references. ...
    (comp.lang.perl.misc)
  • Re: future of lazy programming
    ... evaluation semantics will loop with strict evaluation. ... rather than strict vs lazy evaluation. ...
    (comp.lang.functional)
  • Re: reg exp
    ... So in my while loop I say if line not like ANS or ANR using operator!~ ... looked in my learning perl and programming perl books and did not find this ... which sums up the number read into $total. ... > use strict 'subs'; ...
    (perl.beginners)
  • Re: target practice -- lottery numbers
    ... when you generate a number and then loop through the list to ... undef values, to silence the warnings. ... If there are three values in the array, ... Here's you strict problem. ...
    (perl.beginners)