use strict; use warnings; use Benchmark 'cmpthese'; undef $/; my $str = ; my @regexen = ( qr/foo/, qr/bar/ ); cmpthese(-2, { alternation => sub { my @m = $str =~ /(foo|bar)/g }, map => sub { my @m = map $str =~ /$_/g, @regexen }, loop => sub { my @m; push @m, $str =~ /$_/g for @regexen }, }); cmpthese(-2, { alt_s => sub { my $s2 = $str; $s2 =~ s/(foo|bar)//g }, loop_s => sub { my $s2 = $str; $s2 =~ s/$_//g for @regexen} }); __DATA__ This string contains foo and bar for fools and bards and I pity the foo who bars the way #### Rate alternation map loop alternation 26962/s -- -61% -64% map 68918/s 156% -- -9% loop 75551/s 180% 10% -- Rate alt_s loop_s alt_s 32434/s -- -20% loop_s 40707/s 26% -- #### Rate alternation loop map alternation 26569/s -- -56% -60% loop 60015/s 126% -- -10% map 66863/s 152% 11% -- Rate alt_s loop_s alt_s 31355/s -- -19% loop_s 38857/s 24% --