if: ($var eq "A" || $var eq 'B' || $var eq 'C')
ihash: { A=>1, B=>1, C=>1 }->{$var}
shash: $hash{$var}
re: $var =~ /^(?:A|B|C)\z/
Benchmarking no matches
Rate ihash re if shash
ihash 328339/s -- -79% -81% -86%
re 1557274/s 374% -- -10% -33%
if 1738308/s 429% 12% -- -25%
shash 2307394/s 603% 48% 33% --
Benchmarking match A
Rate ihash re shash if
ihash 333900/s -- -78% -83% -88%
re 1532032/s 359% -- -21% -43%
shash 1946689/s 483% 27% -- -28%
if 2707780/s 711% 77% 39% --
Benchmarking match B
Rate ihash re shash if
ihash 324969/s -- -78% -84% -84%
re 1473476/s 353% -- -26% -28%
shash 2004671/s 517% 36% -- -2%
if 2053900/s 532% 39% 2% --
Benchmarking match C
Rate ihash re if shash
ihash 335514/s -- -76% -81% -83%
re 1392243/s 315% -- -20% -28%
if 1744756/s 420% 25% -- -9%
shash 1920959/s 473% 38% 10% --
####
use strict;
use warnings;
use Benchmark qw( cmpthese );
use Data::Dumper qw( Dumper );
use List::Util qw( max );
use constant REPS => $ARGV[0];
use constant COUNT => $ARGV[1];
{
my $test_if = q{
my $rv = ($var eq "A" || $var eq 'B' || $var eq 'C');
};
my $test_ihash = q{
my $rv = { A=>1, B=>1, C=>1 }->{$var};
};
local our %hash = ( A=>1, B=>1, C=>1 );
my $test_shash = q{
our %hash;
my $rv = $hash{$var};
};
my $test_re = q{
my $rv = $var =~ /^(?:A|B|C)\z/;
};
my %tests = (
if => $test_if,
ihash => $test_ihash,
shash => $test_shash,
re => $test_re,
);
my $tnl = max map length, keys %tests;
foreach (values %tests) {
$_ = q{
use strict;
use warnings;
our $var;
} . $_;
}
{
print("Compilation tests\n");
print("\n");
local our $var = 'moo';
my $exit;
foreach my $test (sort keys %tests) {
printf('%-*s ', $tnl+1, "$test:");
eval($tests{$test});
my $e = $@;
if ($@) {
chomp($e);
print("Compile error: $e\n");
$exit = 1;
} else {
print("Compiles ok\n");
}
}
exit(1) if $exit;
}
print("\n");
print("\n");
{
print("Benchmarking no matches\n");
print("\n");
local our $var = 'D';
my $exit;
foreach my $test (sort keys %tests) {
printf('%-*s ', $tnl+1, "$test:");
my $rv = eval($tests{$test} . ';$rv');
if ($rv) {
local $Data::Dumper::Terse = 1;
print("Runs error: ", Dumper($test));
$exit = 1;
} else {
print("Runs ok\n");
}
}
last if $exit;
for (1..REPS) {
print("\n");
cmpthese(COUNT, \%tests);
}
}
for (qw( A B C )) {
print("\n");
print("\n");
local our $var = $_;
print("Benchmarking match $var\n");
print("\n");
my $exit;
foreach my $test (sort keys %tests) {
printf('%-*s ', $tnl+1, "$test:");
my $rv = eval($tests{$test} . ';$rv');
if ($rv) {
print("Runs ok\n");
} else {
local $Data::Dumper::Terse = 1;
print("Runs error: ", Dumper($test));
$exit = 1;
}
}
last if $exit;
for (1..REPS) {
print("\n");
cmpthese(COUNT, \%tests);
}
}
}
####
>perl -v
This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 25 registered patches, see perl -V for more detail)
...
Binary build 817 [257965] provided by ActiveState http://www.ActiveState.com
Built Mar 20 2006 17:54:25
...
>perl script.pl 3 -3
Compilation tests
shash: Compiles ok
if: Compiles ok
ihash: Compiles ok
re: Compiles ok
Benchmarking no matches
shash: Runs ok
if: Runs ok
ihash: Runs ok
re: Runs ok
Rate ihash re if shash
ihash 328339/s -- -79% -81% -86%
re 1557274/s 374% -- -10% -33%
if 1738308/s 429% 12% -- -25%
shash 2307394/s 603% 48% 33% --
Rate ihash re if shash
ihash 341005/s -- -77% -81% -84%
re 1508921/s 342% -- -16% -30%
if 1795368/s 426% 19% -- -16%
shash 2142666/s 528% 42% 19% --
Rate ihash re if shash
ihash 334628/s -- -78% -81% -86%
re 1508564/s 351% -- -15% -37%
if 1778965/s 432% 18% -- -26%
shash 2405841/s 619% 59% 35% --
Benchmarking match A
shash: Runs ok
if: Runs ok
ihash: Runs ok
re: Runs ok
Rate ihash re shash if
ihash 333900/s -- -78% -83% -88%
re 1532032/s 359% -- -21% -43%
shash 1946689/s 483% 27% -- -28%
if 2707780/s 711% 77% 39% --
Rate ihash re shash if
ihash 323524/s -- -80% -84% -88%
re 1602110/s 395% -- -19% -42%
shash 1990024/s 515% 24% -- -28%
if 2771856/s 757% 73% 39% --
Rate ihash re shash if
ihash 337866/s -- -79% -83% -87%
re 1628208/s 382% -- -17% -36%
shash 1970859/s 483% 21% -- -23%
if 2543785/s 653% 56% 29% --
Benchmarking match B
shash: Runs ok
if: Runs ok
ihash: Runs ok
re: Runs ok
Rate ihash re shash if
ihash 324969/s -- -78% -84% -84%
re 1473476/s 353% -- -26% -28%
shash 2004671/s 517% 36% -- -2%
if 2053900/s 532% 39% 2% --
Rate ihash re shash if
ihash 335927/s -- -78% -83% -84%
re 1520882/s 353% -- -25% -28%
shash 2020630/s 502% 33% -- -4%
if 2100467/s 525% 38% 4% --
Rate ihash re if shash
ihash 323473/s -- -78% -84% -84%
re 1438394/s 345% -- -29% -30%
if 2022373/s 525% 41% -- -1%
shash 2045609/s 532% 42% 1% --
Benchmarking match C
shash: Runs ok
if: Runs ok
ihash: Runs ok
re: Runs ok
Rate ihash re if shash
ihash 335514/s -- -76% -81% -83%
re 1392243/s 315% -- -20% -28%
if 1744756/s 420% 25% -- -9%
shash 1920959/s 473% 38% 10% --
Rate ihash re if shash
ihash 329957/s -- -77% -81% -82%
re 1413031/s 328% -- -17% -23%
if 1707320/s 417% 21% -- -7%
shash 1834897/s 456% 30% 7% --
Rate ihash re if shash
ihash 335803/s -- -77% -81% -83%
re 1452770/s 333% -- -17% -26%
if 1747118/s 420% 20% -- -11%
shash 1967719/s 486% 35% 13% --