in reply to string pattern match, limited to first 1000 characters?
Basically I'm looking for a better method than creating a smaller test string with substr(0, 1000).
That looks like micro-optimization. Taking jwkrahn's and GrandFather's propositions:
#!/usr/bin/perl use Benchmark qw(cmpthese); $substr = join('',a..j); $str = $substr x 90 . 'hTmL'. $substr x 2000; print "length of search string: ",length $str, "\n"; cmpthese(-3, { # \A anchores at the beginning of a string, so no ^. See below (de +merphq) #' regex ' => sub { $str =~ /^\A.{0,996}?html/si; }, ' regex ' => sub { $str =~ /\A.{0,996}?html/si; }, ' substr' => sub { (substr $str, 0, 1000) =~ /html/i; }, #'!regex ' => sub { $str =~ /^\A.{0,996}?sgml/si; }, '!regex ' => sub { $str =~ /\A.{0,996}?sgml/si; }, '!substr' => sub { (substr $str, 0, 1000) =~ /sgml/i; }, }); __END__ length of search string: 20904 Rate regex substr !substr !regex regex 30996/s -- -65% -72% -79% substr 88073/s 184% -- -21% -42% !substr 111975/s 261% 27% -- -26% !regex 150866/s 387% 71% 35% --
What can we deduce from that? Not much. The efficiency of either method seems to depend on whether the searched pattern is contained in the string. The results may vary with the position the pattern in the string.
More important, even the "slowest" of these searches performs at a rate of ~31000/second. How many searches do you have to do in what time? In what context? How does the rest of your code perform?
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|
|---|