Created
April 4, 2018 11:07
-
-
Save haukex/62233ecc427b8c0e96ba011ddefd11e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
use strict; | |
use Benchmark 'cmpthese'; | |
use List::Util 'first'; | |
my @tests = ( | |
{ nrand=> 100, nlook=> 10, match=>'beginning' }, | |
{ nrand=> 100, nlook=> 100, match=>'beginning' }, | |
{ nrand=>10_000, nlook=> 100, match=>'beginning' }, | |
{ nrand=> 100, nlook=>10_000, match=>'beginning' }, | |
{ nrand=>10_000, nlook=>10_000, match=>'beginning' }, | |
{ nrand=> 100, nlook=> 10, match=>'end' }, | |
{ nrand=> 100, nlook=> 100, match=>'end' }, | |
{ nrand=>10_000, nlook=> 100, match=>'end' }, | |
{ nrand=> 100, nlook=>10_000, match=>'end' }, | |
{ nrand=>10_000, nlook=>10_000, match=>'end' }, | |
); | |
for my $test (@tests) { | |
print "random pool size: $test->{nrand}, number of lookups: " | |
, "$test->{nlook},\n\twill find match at $test->{match} of array\n"; | |
my @random_pool = map { int(rand($test->{nrand}))+1 } 1..$test->{nrand}; | |
my $value = $random_pool[ $test->{match} eq 'end' ? -1 : 0 ]; | |
my $m; | |
cmpthese( -10, { | |
grep => sub { # https://stackoverflow.com/a/49635111/9300627 | |
for (1..$test->{nlook}) { | |
$m++ if grep { $_ == $value } @random_pool; | |
} | |
}, | |
first => sub { # https://stackoverflow.com/a/49635111/9300627 | |
for (1..$test->{nlook}) { | |
$m++ if defined( first { $_ == $value } @random_pool ); | |
} | |
}, | |
regex => sub { # https://stackoverflow.com/a/49635111/9300627 | |
my $pool_str = " @random_pool "; | |
for (1..$test->{nlook}) { | |
$m++ if $pool_str =~ / $value /; | |
} | |
}, | |
hash => sub { # https://stackoverflow.com/a/49635333/9300627 | |
my %is_in_pool = map { $_ => 1 } @random_pool; | |
for (1..$test->{nlook}) { | |
$m++ if $is_in_pool{$value}; | |
} | |
}, | |
}); | |
print "\n"; | |
} | |
__END__ | |
random pool size: 100, number of lookups: 10, | |
will find match at beginning of array | |
Rate grep hash first regex | |
grep 35962/s -- -34% -80% -86% | |
hash 54210/s 51% -- -70% -78% | |
first 183266/s 410% 238% -- -27% | |
regex 250755/s 597% 363% 37% -- | |
random pool size: 100, number of lookups: 100, | |
will find match at beginning of array | |
Rate grep first regex hash | |
grep 3473/s -- -81% -92% -92% | |
first 18251/s 426% -- -57% -59% | |
regex 42426/s 1122% 132% -- -4% | |
hash 43995/s 1167% 141% 4% -- | |
random pool size: 10000, number of lookups: 100, | |
will find match at beginning of array | |
Rate grep hash first regex | |
grep 37.0/s -- -92% -96% -99% | |
hash 461/s 1145% -- -46% -92% | |
first 850/s 2195% 84% -- -86% | |
regex 5983/s 16055% 1198% 604% -- | |
random pool size: 100, number of lookups: 10000, | |
will find match at beginning of array | |
Rate grep first regex hash | |
grep 36.2/s -- -80% -92% -98% | |
first 184/s 407% -- -60% -91% | |
regex 463/s 1179% 152% -- -76% | |
hash 1961/s 5318% 969% 324% -- | |
random pool size: 10000, number of lookups: 10000, | |
will find match at beginning of array | |
Rate grep first hash regex | |
grep 0.331/s -- -94% -100% -100% | |
first 5.60/s 1589% -- -98% -99% | |
hash 364/s 109677% 6398% -- -14% | |
regex 422/s 127352% 7445% 16% -- | |
random pool size: 100, number of lookups: 10, | |
will find match at end of array | |
Rate grep hash first regex | |
grep 33659/s -- -34% -63% -82% | |
hash 51105/s 52% -- -44% -73% | |
first 91134/s 171% 78% -- -52% | |
regex 188790/s 461% 269% 107% -- | |
random pool size: 100, number of lookups: 100, | |
will find match at end of array | |
Rate grep first regex hash | |
grep 3474/s -- -17% -80% -92% | |
first 4195/s 21% -- -76% -90% | |
regex 17789/s 412% 324% -- -59% | |
hash 42982/s 1137% 925% 142% -- | |
random pool size: 10000, number of lookups: 100, | |
will find match at end of array | |
Rate grep first regex hash | |
grep 32.9/s -- -40% -86% -93% | |
first 54.9/s 67% -- -76% -88% | |
regex 228/s 592% 315% -- -50% | |
hash 457/s 1290% 733% 101% -- | |
random pool size: 100, number of lookups: 10000, | |
will find match at end of array | |
Rate grep first regex hash | |
grep 35.5/s -- -40% -86% -98% | |
first 59.1/s 66% -- -76% -97% | |
regex 248/s 600% 320% -- -87% | |
hash 1954/s 5404% 3209% 687% -- | |
random pool size: 10000, number of lookups: 10000, | |
will find match at end of array | |
s/iter grep first regex hash | |
grep 3.10 -- -51% -90% -100% | |
first 1.51 106% -- -79% -100% | |
regex 0.318 875% 373% -- -99% | |
hash 2.76e-03 112587% 54597% 11457% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment