UPDATE: typo inflates the results here, study does not produce the improvement shown here. But it does do something, not nothing, so I'm pretty sure it's not a no-op.
C:\perl>perl -v
This is perl 5, version 20, subversion 1 (v5.20.1) built for MSWin32-x
+86-multi-thread-64int
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Benchmark qw(cmpthese);
my @messages = <DATA>;
cmpthese(1000000, {
'mca_func' => sub {
my $out = mca_substitute($_) foreach (@messages);
},
'toolic_func' => sub {
my $out = toolic_substitute($_) foreach (@messages);
},
'study_func' => sub {
my $out = study_substitute($_) for each (@messages);
},
});
sub toolic_substitute {
my $message = shift;
$message =~ s/\{\\d(\d+)\}/sprintf "%0${1}d", int(rand(10**$1))/ge
+;
return $message;
}
sub study_substitute {
my $message = shift; study $message;
$message =~ s/\{\\d(\d+)\}/sprintf "%0${1}d", int(rand(10**$1))/ge
+;
return $message;
}
sub mca_substitute {
my $message = shift;
$message =~ s/\{\\d(\d+)\}/myreplace($1)/ge;
return $message;
}
sub myreplace {
return '' unless $_[0];
my $string = '';
$string .= int(rand 10) for (1..$_[0]);
return $string;
}
__DATA__
This is a message! {\d3} --- {\d2}
Another {\d3} Message with {\d5}
A {\d1} little {\d6} longer string {\d3}
C:\perl>perl testpm.pl
Rate mca_func toolic_func study_func
mca_func 34744/s -- -32% -78%
toolic_func 50795/s 46% -- -68%
study_func 157505/s 353% 210% --
C:\perl>
Not bad for a no-op.