[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] for loop quickie



Daniel Linder wrote:
Ok, Jay -- which one is faster? I am pretty sure it's the second one, but I'm interested in how much.

(Thought I'd ask since you're always benchmarking ...)

Looks like "after" is almost twice as efficient... Interesting how my wallclock secs bounced around. I guess my Mac was busy doing something else a couple times. :)

j


$ cat j.pl
use Benchmark qw(:all);

my @row = (a..z);
timethese(100000, {
  'Before' => sub { before() },
  'After'  => sub { after() }
});


sub before {
 for ($j = 0; $j < @row; $j++) {
   $row[$j] =~ s/[^ -~]//g;
   $row[$j] =~ s/\|/:/g;
 }
}

sub after {
 for (@row) {
   s/[^ -~]//g;
   s/\|/:/g;
 }
}

$ perl j.pl
Benchmark: timing 100000 iterations of After, Before...
    After:  2 wallclock secs ( 1.42 usr +  0.01 sys =  1.43 CPU) @ 69930.07/s (n=100000)
   Before:  2 wallclock secs ( 2.53 usr +  0.00 sys =  2.53 CPU) @ 39525.69/s (n=100000)
$ perl j.pl
Benchmark: timing 100000 iterations of After, Before...
    After:  1 wallclock secs ( 1.43 usr +  0.01 sys =  1.44 CPU) @ 69444.44/s (n=100000)
   Before:  3 wallclock secs ( 2.51 usr +  0.00 sys =  2.51 CPU) @ 39840.64/s (n=100000)
$ perl j.pl
Benchmark: timing 100000 iterations of After, Before...
    After:  1 wallclock secs ( 1.42 usr +  0.01 sys =  1.43 CPU) @ 69930.07/s (n=100000)
   Before:  1 wallclock secs ( 2.51 usr +  0.00 sys =  2.51 CPU) @ 39840.64/s (n=100000)
$ perl j.pl
Benchmark: timing 100000 iterations of After, Before...
    After:  1 wallclock secs ( 1.43 usr +  0.01 sys =  1.44 CPU) @ 69444.44/s (n=100000)
   Before:  1 wallclock secs ( 2.52 usr +  0.00 sys =  2.52 CPU) @ 39682.54/s (n=100000)