Excellent++. With 100 keys looked up in a string that contains them all in random order. The big regex beats out the map option, but your invert-the-logic hash lookup wins easily (2 1/2 x quicker than the regex).

By the time you get to 1,000 keys, the map overtakes the regex but yours is an even clearer winner (nearly 30x faster).

By the time you get to 10,000 keys, the margin is over 250x as fast.

Results

s/iter regex map hash regex 119 -- -71% -100% map 34.7 243% -- -99% hash 0.443 26705% 7715% -- 10000:10000:10000

I think its better to use the magical split ' ', rather than /\s/, as the latter will generate nulls for consecutive whitespace and even /\s+/ will generate one null if there is any leading whitespace.

print join '|', split /\s/, ' the quick brown fox j +umps ' |||||||||the||quick||||brown||||fox|||||jumps print join '|', split /\s+/, ' the quick brown fox +jumps ' |the|quick|brown|fox|jumps print join '|', split ' ', ' the quick brown fox ju +mps ' the|quick|brown|fox|jumps

Benchmark

#! perl -slw use strict; use Benchmark qw[ cmpthese ]; sub rndStr ($@) { join '', @_[ map{ rand @_ } 0 .. shift ] } sub shuffle (\@) { my $r=pop; $a = $_ + rand @{$r} - $_ and @$r[$_, $a +] = @$r[$a, $_] for 0..$#{$r}; } our @words = map{ rndStr 3 + rand 6, 'A' .. 'Z', 'a'..'z' } 1 .. 10000 +; our $string = join ' ', @words; shuffle @words; our %hash; @hash{@words} = (); our $regex = '(?x: \b' . join('\b | \b', map quotemeta, @words) . '\b + )'; our (@regex, @map, @hash); cmpthese( -100, { regex => q[ @regex = $string =~ m[($regex)]g; ], map => q[ @map = map{ my $var = quotemeta $_; $string =~ m[\b($v +ar)\b] } @words; ], hash => q[ @hash = map{ exists $hash{$_} } split ' ', $string; ], }); print scalar @regex, ':', scalar @map, ':', scalar @hash; __END__ D:\Perl\test>temp Rate map regex hash map 31.5/s -- -56% -87% regex 71.7/s 128% -- -71% hash 250/s 696% 249% -- 100:100:100 D:\Perl\test>temp Rate regex map hash regex 0.828/s -- -62% -97% map 2.17/s 163% -- -91% hash 24.6/s 2871% 1031% -- 1000:1000:1000 D:\Perl\test>temp (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) s/iter regex map hash regex 119 -- -71% -100% map 34.7 243% -- -99% hash 0.443 26705% 7715% -- 10000:10000:10000

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

In reply to Re: Re: how to find hash keys in a string ? by BrowserUk
in thread how to find hash keys in a string ? by Anonymous Monk

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.