Actually compared to the translate (counting) technique, for a case sensitive match McDarren's solution using the equivelent regex technique is slightly faster, but it's slightly slower if using a case insensitive match. Case sensitivity makes no significant difference to the translate.
For the sake of code clarity I'd actually go with the multiple regex solution, but offered the translate solution because the technique gets forgotten about somewhat.
use strict; use warnings; use Benchmark qw(cmpthese); my @words = qw(perl purile pretty reputation reputable longwithoutanym +atch); cmpthese (-1, { tr => \&do_tr, tri => \&do_tri, m => \&do_m, mi => \&do_mi, } ); sub do_tr { my $matched = 0; $matched += tr/p// && tr/e// && tr/r// && tr/l// for @words; return $matched; } sub do_tri { my $matched = 0; $matched += tr/pP// && tr/eE// && tr/rR// && tr/lL// for @words; return $matched; } sub do_m { my $matched = 0; $matched += m/p/i && /e/i && /r/i && /l/i for @words; return $matched; } sub do_mi { my $matched = 0; $matched += m/p/ && /e/ && /r/ && /l/ for @words; return $matched; } --------- 8< -------------------- Rate m tri tr mi m 98443/s -- -15% -16% -22% tri 115597/s 17% -- -1% -9% tr 116736/s 19% 1% -- -8% mi 126385/s 28% 9% 8% --
In reply to Re^3: A Quick Regex Question
by GrandFather
in thread A Quick Regex Question
by chinamox
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |