#!/usr/bin/perl
use strict;
use warnings;
use Benchmark 'cmpthese';
local $" = '|';
my $target = join '', map chr(97 + rand 26), 1 .. 100000;
my @patterns = map {join '', map chr(97 + rand 26), 1 .. 5 } 1 .. 100;
my @res = map qr/$_/, @patterns;
my $whole_pat = "@patterns";
my $whole_re = qr/@patterns/;
cmpthese(-5, {
'inline' => sub {$target =~ /@patterns/},
'inline-o' => sub {$target =~ /@patterns/o},
'grep_str' => sub {return 1 if grep $target =~ $_, @patterns},
'grep_RE' => sub {return 1 if grep $target =~ $_, @res},
'whole_pat' => sub {$target =~ /$whole_pat/},
'whole_pat-o' => sub {$target =~ /$whole_pat/o},
'whole_re' => sub {$target =~ $whole_re},
});
####
Rate grep_str grep_RE inline inline-o whole_pat-o whole_pat whole_re
grep_str 96.6/s -- -2% -67% -67% -67% -67% -67%
grep_RE 99.1/s 3% -- -67% -67% -67% -67% -67%
inline 296/s 207% 199% -- -0% -0% -0% -0%
inline-o 296/s 207% 199% 0% -- -0% -0% -0%
whole_pat-o 297/s 207% 199% 0% 0% -- -0% -0%
whole_pat 297/s 207% 200% 0% 0% 0% -- 0%
whole_re 297/s 207% 200% 0% 0% 0% 0% --
####
Rate grep_str grep_RE inline inline-o whole_re whole_pat whole_pat-o
grep_str 97.5/s -- -2% -94% -94% -94% -94% -94%
grep_RE 99.8/s 2% -- -94% -94% -94% -94% -94%
inline 1686/s 1628% 1589% -- -0% -1% -1% -1%
inline-o 1688/s 1630% 1591% 0% -- -1% -1% -1%
whole_re 1707/s 1650% 1610% 1% 1% -- -0% -0%
whole_pat 1707/s 1650% 1610% 1% 1% 0% -- -0%
whole_pat-o 1707/s 1650% 1610% 1% 1% 0% 0% --
####
Rate grep_str grep_RE inline inline-o whole_pat-o whole_pat whole_re
grep_str 169/s -- -4% -46% -46% -46% -46% -46%
grep_RE 177/s 5% -- -43% -43% -44% -44% -44%
inline 312/s 85% 77% -- -0% -0% -0% -0%
inline-o 312/s 85% 77% 0% -- -0% -0% -0%
whole_pat-o 313/s 85% 77% 0% 0% -- -0% -0%
whole_pat 313/s 86% 77% 0% 0% 0% -- 0%
whole_re 313/s 86% 77% 0% 0% 0% 0% --