If you

  1. sort your search patterns by length and then value (reversed);
  2. chop characters off the string one at a time until it is less than the longest/highest string;
  3. then descend down the array comparing against each string in turn;
  4. quit the loop when you find a match, or loop and repeat;

The worst case time I've seen is always less than 2 milliseconds. How does that compare with the regex solution?

#! perl -slw use strict; use Math::Random::MT qw[ rand ]; use Benchmark::Timer; my @array = sort{ length( $b ) <=> length( $a ) || $b cmp $a } qw[ 0123456 012345 01234], map int rand( 123456789 ), 1 .. 5022; my $str = '012345666666'; my $T = new Benchmark::Timer; $T->start( '11-char string in 5025 possibles' ); my $copy = $str; my $n = 0; while( $n < $#array and $copy ne $array[ $n ] ) { chop $copy while $copy gt $array[ $n ]; $n++ while $copy lt $array[ $n ]; last if $copy eq $array[ $n ]; } if( length $copy ) { print "Best match found $copy (at posn: $n )"; } else { print "No match found"; } $T->stop( '11-char string in 5025 possibles' ); $T->report; print for grep defined, @array[ $n - 10 .. $n + 10 ]; __END__ C:\test>583928 Best match found 0123456 (at posn: 4975 ) 1 trial of 11-char string in 5025 possibles (1.621ms total) 1166599 1157595 1146996 1065416 1063127 1063055 1052870 1034509 1021521 1004863 0123456 997625 973306 962972 943968 911619 881691 873333 838373 832949 788294

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Regex only the longes Value , and possible speedup Problem by BrowserUk
in thread Regex only the longes Value , and possible speedup Problem by ultibuzz

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.