in reply to Re: how to find hash keys in a string ?
in thread how to find hash keys in a string ?

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