On Sep 29, Hanson, Rob said:

>I ran some benchmarks.
>
>The two-liner outperformed the one-liners by a 10 to 1 ratio.  Code and
>results below.
>
>Benchmark: timing 100000 iterations of OneLine, OneLine2, TwoLines...
>  OneLine: 41 wallclock secs (39.30 usr +  0.00 sys = 39.30 CPU) @ 2544.79/s
>  OneLine2: 34 wallclock secs (32.58 usr +  0.00 sys = 32.58 CPU) @
>3069.56/s
>  TwoLines:  3 wallclock secs ( 2.58 usr +  0.00 sys =  2.58 CPU) @
>38789.76/s

You'll get a better representation with the following benchmark:

  #!/usr/bin/perl

  use Benchmark 'cmpthese';

  my $s = "   " . join("  ", ('foo') x 100) . "   ";

  cmpthese(-5, {
    capture => \&capture,
    alt => \&alt,
    two => \&two,
    two_rev => \&two_rev,
  });

  sub capture {
    my $copy = $s;
    $copy =~ s/^\s*(.*?)\s*$/$1/s;
  }

  sub alt {
    my $copy = $s;
    $copy =~ s/^\s+|\s+$//g;
  }

  sub two {
    my $copy = $s;
    s/^\s+//, s/\s+$// for $copy;
  }

  sub two_rev {
    my $copy = $s;
    $copy =~ s/^\s+//;
    ($copy = reverse $copy) =~ s/^\s+//;
    $copy = reverse $copy;
  }

The output is thus:

Benchmark: running alt, capture, two, two_rev for at least 5 CPU seconds
       alt:  5 wallclock secs @   4163.86/s (n= 21777)
   capture:  5 wallclock secs @   4876.60/s (n= 25846)
       two:  6 wallclock secs @  24841.44/s (n=130666)
   two_rev:  5 wallclock secs @ 148958.27/s (n=774583)

            Rate     alt capture     two two_rev
alt       4164/s      --    -15%    -83%    -97%
capture   4877/s     17%      --    -80%    -97%
two      24841/s    497%    409%      --    -83%


Notice the string this time.  It's got embedded whitespace.  This causes
the '\s+$' regex to perform HORRIBLY.  If you want to see why, try this:

  perl -mre=debug -e '"abc  def  ghi  jkl  mno" =~ /\s+$/'

Notice that the string "abc  def...  mno" doesn't end in whitespace, but
that the engine checks for /\s+$/ at each chunk of whitespace.  Icky
awful.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to