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}

In reply to Re: string pattern match, limited to first 1000 characters? by shmem
in thread string pattern match, limited to first 1000 characters? by ManFromNeptune

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.