s/iter regex map hash
regex 119 -- -71% -100%
map 34.7 243% -- -99%
hash 0.443 26705% 7715% --
10000:10000:10000
####
print join '|', split /\s/, ' the quick brown fox jumps '
|||||||||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 jumps '
the|quick|brown|fox|jumps
####
#! 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($var)\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