davido,
You are correct as the following modified code shows:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(:all);
my $length = (int rand 100) + 100;
my @char = ( 'a' .. 'c' , '+' , 'd' .. 'f' );
my $string;
$string .= $char[ int rand 7 ] for 0 .. $length ;
my $count = -5;
my $results = timethese ( $count,
{
'transliterate' => sub { my $foo = $string; $foo =~ tr/
++/ /; },
'substitution' => sub { my $bar = $string; $bar =~s/\+
+/ /g; },
},
'none'
);
cmpthese( $results ) ;
__END__
Rate substitution transliterate
substitution 124624/s -- -84%
transliterate 784349/s 529% --
Cheers -
L~R