in reply to Re: Re: Re: problems with tr///;
in thread problems with tr///;

<voice type="marvin_the_martian">Greetings earth specimen. You have made a naughty Benchmark goof, so I shall have to blow you up</voice> ;-)

Note that $var is modified by the subs, so calling them over and over again is pointless. This is very misleading because the "work" you are attempting to benchmark is only being done once. Your tests essentially behave like this:

$var = 'abc'; $var =~ s/b//; # $var is now 'ac' $var =~ s/b//; # still 'ac' even before the s/// $var =~ s/b//; # yet another attempt to remove 'b' from 'ac' $var =~ s/b//; # well, you get the point.... .... etc.
Here is a better benchmark (on 5.6.0) that restores my faith in tr///:
#!/usr/bin/perl -wT use strict; use Benchmark; our $var; $var = '\a\\aa\\\aaa\\\\aaaa'x100; ## rerun the original benchmarks to provide a baseline timethese(100000, { 'old s' => sub { $var =~ s/\\//g }, 'old tr' => sub { $var =~ tr/\\//d }, }); ## try some new benchmarks that reinitialize $var each time timethese(50000, { 'new s' => sub { $var = '\a\\aa\\\aaa\\\\aaaa'x100; $var =~ s/\\/ +/g }, 'new tr' => sub { $var = '\a\\aa\\\aaa\\\\aaaa'x100; $var =~ tr/\\ +//d }, }); =OUTPUT Benchmark: timing 100000 iterations of old s, old tr... old s: 1 wallclock secs ( 0.56 usr + 0.00 sys = 0.56 CPU) @ 17 +8571.43/s (n=100000) old tr: 2 wallclock secs ( 1.23 usr + 0.00 sys = 1.23 CPU) @ 81 +300.81/s (n=100000) Benchmark: timing 50000 iterations of new s, new tr... new s: 43 wallclock secs (30.81 usr + 0.02 sys = 30.83 CPU) @ 16 +21.80/s (n=50000) new tr: 3 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 24 +271.84/s (n=50000)

-Blake

Replies are listed 'Best First'.
Re: Re4: problems with tr///;
by cforde (Monk) on Sep 27, 2001 at 20:48 UTC
    <voice type="yosemite_sam">Great horny toads. You"re right.</voice>

    Have fun,
    Carl Forde