sub regex { my $cnt=0; for (@words) { ++$cnt if /^abcb$/i; } return $cnt; } #### In 4096 words, 16 are 'abcb' Rate uccmp regex uccmp 1192/s -- -5% regex 1255/s 5% -- In 4096 words, 0 are 'abcb' Rate uccmp regex uccmp 1225/s -- -3% regex 1264/s 3% -- In 4096 words, 4096 are 'abcb' Rate regex uccmp regex 970/s -- -23% uccmp 1260/s 30% -- #### use strict; use warnings; use Benchmark qw(:all); my @words; my @letters = qw( A B C D a b c d ); build_words(\@letters, \@letters, \@letters, \@letters); compare(); @letters = qw( d e f g h i k l ); build_words(\@letters, \@letters, \@letters, \@letters); compare(); @letters = qw( B B B B b b b b ); build_words( [qw(A A A A a a a a)], \@letters, [qw(C C C C c c c c)], \@letters); compare(); sub build_words { @words=(); my ($rA, $rB, $rC, $rD) = @_; for my $a (@$rA) { for my $b (@$rB) { for my $c (@$rC) { for my $d (@$rD) { push @words, "$a$b$c$d\n"; } } } } } sub compare { my $v = @words; my $t = regex(); my $u = uccmp(); if ($t != $u) { die "Functions don't return the same value! regex=$t, uccmp=$u\n"; } print "In $v words, $t are 'abcb'\n"; cmpthese(-5, { regex => sub { regex() }, uccmp => sub { uccmp() }, } ); } sub regex { my $cnt=0; for (@words) { ++$cnt if /^abcb$/i; } return $cnt; } sub uccmp { my $cnt=0; for (@words) { chomp; ++$cnt if uc($_) eq 'ABCB'; } return $cnt; } #### In 4096 words, 16 are 'abcb' Rate uccmp regex uccmp 812/s -- -32% regex 1197/s 47% -- In 4096 words, 0 are 'abcb' Rate uccmp regex uccmp 861/s -- -31% regex 1255/s 46% -- In 4096 words, 4096 are 'abcb' Rate uccmp regex uccmp 856/s -- -11% regex 964/s 13% -- #### use strict; use warnings; use Benchmark qw(:all); my @words; my @letters = qw( A B C D a b c d ); build_words(\@letters, \@letters, \@letters, \@letters); compare(); @letters = qw( d e f g h i k l ); build_words(\@letters, \@letters, \@letters, \@letters); compare(); @letters = qw( B B B B b b b b ); build_words( [qw(A A A A a a a a)], \@letters, [qw(C C C C c c c c)], \@letters); compare(); sub build_words { @words=(); my ($rA, $rB, $rC, $rD) = @_; for my $a (@$rA) { for my $b (@$rB) { for my $c (@$rC) { for my $d (@$rD) { push @words, "key=$a$b$c$d\n"; } } } } } sub compare { my $v = @words; my $t = regex(); my $u = uccmp(); if ($t != $u) { die "Functions don't return the same value! regex=$t, uccmp=$u\n"; } print "In $v words, $t are 'abcb'\n"; cmpthese(-5, { regex => sub { regex() }, uccmp => sub { uccmp() }, } ); } sub regex { my $cnt=0; for (@words) { ++$cnt if /^[^=]*=abcb(?:=|$)/i; } return $cnt; } sub uccmp { my $cnt=0; for (@words) { chomp; ++$cnt if uc( (split /=/)[1] ) eq 'ABCB'; } return $cnt; } #### In 4096 words, 16 are 'abcb' Rate uccmp regex uccmp 200/s -- -74% regex 767/s 283% -- In 4096 words, 0 are 'abcb' Rate uccmp regex uccmp 200/s -- -74% regex 756/s 278% -- In 4096 words, 4096 are 'abcb' Rate uccmp regex uccmp 206/s -- -66% regex 603/s 193% --