<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


In reply to Re4: problems with tr///; by blakem
in thread problems with tr///; by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.