use Benchmark 'cmpthese';
$iterations = 1000000;
%re = (
foo => 'foo1',
bar => 'bar1',
);
$re = join '|', keys %re;
$re = qr/($re)/;
$name1 = "RE";
$code1 = << 'END_CODE1';
$_ = 'foo bar';
s/$re/$re{$1}/g;
END_CODE1
$name2 = "Loop";
$code2 = << 'END_CODE2';
$_ = 'foo bar';
for my $sub( keys %re ) {
s/$sub/$re{$sub}/g;
}
END_CODE2
cmpthese( $iterations, {$name1 => $code1, $name2 => $code2} );
__END__
Benchmark: timing 1000000 iterations of Loop, RE...
Loop: 24 wallclock secs (24.77 usr + 0.00 sys = 24.77 CPU) @ 40377.94/s (n=1000000)
RE: 8 wallclock secs ( 7.27 usr + 0.00 sys = 7.27 CPU) @ 137551.58/s (n=1000000)
Rate Loop RE
Loop 40378/s -- -71%
RE 137552/s 241% --
####
$_ = 'foo bar' x 2000;
Benchmark: timing 10000 iterations of Loop, RE...
Loop: 20 wallclock secs (19.90 usr + 0.00 sys = 19.90 CPU) @ 502.56/s (n=10000)
RE: 30 wallclock secs (29.44 usr + 0.00 sys = 29.44 CPU) @ 339.64/s (n=10000)
Rate RE Loop
RE 340/s -- -32%
Loop 503/s 48% -
##
##
$_ = 'fo ba' x 1000 . 'foo bar';
Benchmark: timing 10000 iterations of Loop, RE...
Loop: 1 wallclock secs ( 0.71 usr + 0.00 sys = 0.71 CPU) @ 14064.70/s (n=10000)
RE: 12 wallclock secs (11.10 usr + 0.00 sys = 11.10 CPU) @ 901.23/s (n=10000)
Rate RE Loop
RE 901/s -- -94%
Loop 14065/s 1461% --