How about a Benchmark where the strings do match?
#!perl -w
use strict;
use Benchmark qw/ cmpthese /;
my $x = 'The Monastery Gates';
my $y = 'ThE MoNaStErY GaTeS';
cmpthese(-(shift),
{
eq => sub{ if (lc $x eq lc $y) {} },
re => sub{ if ($x =~ /^$y$/i) {} },
}
)
__END__
Benchmark: running eq, re, each for at least 3 CPU seconds...
eq: 6 wallclock secs ( 3.04 usr + 0.01 sys = 3.05 CPU) @ 24
+2974.43/s
(n=741072)
re: 8 wallclock secs ( 3.00 usr + 0.00 sys = 3.00 CPU) @ 86
+303.00/s (n=258909)
Rate re eq
re 86303/s -- -64%
eq 242974/s 182% --
That's not all, though. When the strings don't match and $y is shorter, eq comes out about 220% ahead. When $y is longer and the regex optimizer can tell right off that $x is too short for /^$y$/ to match, eq still has a 40% advantage. |