$re = qr/$regex/;
$_ =~ $re;
####
use Benchmark;
sub make_grep {
my $p = $_[0];
eval "sub { grep /$p/o, @_ }";
}
sub make_grep2 {
my $p = $_[0];
sub { grep /$p/o, @_ };
}
$re = '(ob|[tTs])';
$a = make_grep($re);
$b = make_grep2($re);
$pat = qr/$re/;
timethese(100000,
{
a => sub { $a->(qw(this is a test bob and jobe)) },
b => sub { $b->(qw(this is a test bob and jobe)) },
c => sub { grep $pat, qw(this is a test bob and jobe) }
});
####
Benchmark: timing 100000 iterations of a, b, c...
a: 7 wallclock secs ( 5.92 usr + 0.00 sys = 5.92 CPU) @ 16891.89/s (
n=100000)
b: 5 wallclock secs ( 4.57 usr + 0.00 sys = 4.57 CPU) @ 21881.84/s (
n=100000)
c: 0 wallclock secs ( 0.62 usr + 0.00 sys = 0.62 CPU) @ 161290.32/s
(n=100000)