Post your code for this Benchmark and I'll show you where
it went wrong. I'd bet on the variable you tested against
not being in scope inside the benchmark sub/evals.
My results, Linux on an IBM Netfinity (Intel)
Benchmark: running regexp, transl, each for at least 10 CPU seconds...
regexp: 10 wallclock secs (10.59 usr + 0.00 sys = 10.59 CPU) @ 37
+274.69/s (n=394739)
transl: 13 wallclock secs (10.46 usr + 0.00 sys = 10.46 CPU) @ 31
+3981.07/s (n=3284242)
Rate regexp transl
regexp 37275/s -- -88%
transl 313981/s 742% --
That is a significant differential there for this simple
task. A full regexp engine is a big thing to throw at a
lightweight string scan. My benchmark code follows:
use strict;
use Benchmark qw(cmpthese);
use vars qw( $x );
$x = 'This-is-a-test-string-I-just-typed-in-for-fun';
cmpthese (-10, {
'transl' => '$x =~ tr/-/_/; $x =~ tr/_/-/;',
'regexp' => '$x =~ s/-/_/g; $x =~ s/_/-/g;',
} );
Oh yeah, I sure am happy Benchmark exists too. =)
Doh! Update: that assignment was:
$x = 'This_is_a_test_string_I_just_typed_in_for_fun';
It wasn't result impacting, just stupid since it no-ops
half my test. Interestingly, if I change the string to one
with spaces rather than the '-' or '_' I wind up with
regexp being 50-60% faster at doing nothing but scanning
with no changes...
--
$you = new YOU;
honk() if $you->love(perl)
-
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.