<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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|