<voice type="marvin_the_martian"<Oohh, you have made me very skeptical</voice>

So I tried a couple of benchmarks. (I'm using 5.6.1, build 626 from ActiveState.) Here's the code:

use Benchmark; $var = '\a\\aa\\\aaa\\\\aaaa'x100; print "s: "; timethis(100000, '$var =~ s/\\\//g'); # 3 \ for benchmark print "tr: "; timethis(100000, '$var =~ tr/\\\//d');
and here's the results
s: timethis 100000: 1 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 +CPU) @ 195694.72/s (n=100000) tr: timethis 100000: 2 wallclock secs ( 1.68 usr + 0.00 sys = 1.68 +CPU) @ 59453.03/s (n=100000)
You can play with $var and see the effect on the timings. Maybe tr used to be faster, but that's no longer the case.

<soapbox>
It's really beside the point. The point is the right tool for the job. In this case the right tool is the substitution operator. If you wanted to make multiple single character changes in the string then tr is what you want. Note the difference between:

$var =~ tr /ab/cd/; # and $var =~ s /ab/cd/g;
</soapbox>

Have fun,
Carl Forde


In reply to Re: Re: Re: problems with tr///; by cforde
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.