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)
In reply to Re: Re: Re: Re: string manipulation
by extremely
in thread string manipulation
by parsifal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |