You have a valid point in that node i was kind of comparing apples to oranges (Of course that node was about uppercasing input which is a bit different). I must have been having brain dead kind of day. As for your number I changed my benchmark code to this for this specific case to this:
#!/usr/local/bin/perl -w
use strict;
use Benchmark;
my $count = 900000;
## Method number two
sub One {
my $data='for-bar-baz';
$data =~tr/-/_/;
}
## Method number Two
sub Two {
my $data='for-bar-baz';
$data =~s/-/_/g;
}
## We'll test each one, with simple labels
timethese (
$count,
{'Method One TR' => '&One',
'Method Two s'=> '&Two'
}
);
exit;
Which results in the following
Benchmark: timing 500000 iterations of Method One TR, Method Two s...
Method One TR: 2 wallclock secs ( 1.87 usr + 0.00 sys = 1.87 CPU) @
+ 267379.68/s (n=500000)
Method Two s: 5 wallclock secs ( 4.84 usr + 0.00 sys = 4.84 CPU) @
+103305.79/s (n=500000)
FYI- I got these numbers using Perl 5.6.0 on Win32
You are right that this is not as big of difference as 17:1 (Which when I first ran seemed odd to me...) But is still a decent gap.
I got the suggestion about not using S when TR will do from
Effective Perl Programming (co-authored by our own
merlyn). It is a great book with lots of info tweaking your code.
off to update that other node now...
PS- your benchmark doesnt do anything in either case the line:
my $data='for bar baz'; Should be changed to have the char being looked for. Like So:
my $data='for-bar-baz';
-
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.