http://qs1969.pair.com?node_id=576527


in reply to Re: Regex Start Anchor with variables
in thread Regex Start Anchor with variables

Even faster than index() or a regex -- I would imagine -- would be substr() and eq together.

When imagine when you can Benchmark?

use Benchmark 'cmpthese'; my $str1 = 'foobarbazqux'; my $str2 = 'foobar'; my $str2len = length $str2; cmpthese (3e6, { regex => sub { $str1 =~ /\Q$str2/; }, regex2 => sub { $str1 =~ /\Q$str2/o; }, ## probably useless test, +see japhy's comment below index => sub { index $str1, $str2; }, substr => sub { $str2 eq substr $str1, 0, length $str2; }, substr2 => sub { $str2 eq substr $str1, 0, $str2len; }, }); __END__ Rate regex regex2 substr substr2 index regex 1063830/s -- -13% -25% -43% -72% regex2 1229508/s 16% -- -14% -34% -68% substr 1421801/s 34% 16% -- -23% -63% substr2 1851852/s 74% 51% 30% -- -51% index 3797468/s 257% 209% 167% 105% --

The index solution is clearly superior to the others. It's interesting to see the improvements of regex2 and substr2 over their unoptimized counterparts.

Update: I forgot that the OP was matching at the beginning of the string. This is a better benchmark:

use Benchmark 'cmpthese'; my $str1 = 'foobarbazqux'; my $str2 = 'foobar'; my $str2len = length $str2; cmpthese (3e6, { regex => sub { $str1 =~ /^\Q$str2/; }, index => sub { 0 == index $str1, $str2; }, substr => sub { $str2 eq substr $str1, 0, length $str2; }, substr2 => sub { $str2 eq substr $str1, 0, $str2len; }, }); __END__ Rate regex substr substr2 index regex 671141/s -- -59% -65% -71% substr 1630435/s 143% -- -16% -30% substr2 1935484/s 188% 19% -- -17% index 2343750/s 249% 44% 21% --

--
David Serrano