-
-
Save ology/2afd45b55fa79f170b5cc2454bcecf36 to your computer and use it in GitHub Desktop.
Benchmark any vs grep
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 strict; | |
use warnings; | |
use Benchmark; | |
use List::Util 'any'; | |
my $count = shift || 10_000_000; | |
my $needle = 111; | |
my @haystack = (0 .. 999); | |
timethese($count, { | |
grep_it => \&grep_it, | |
any_it => \&any_it, | |
}); | |
sub grep_it { | |
my $x = grep { $needle == $_ } @haystack; | |
} | |
sub any_it { | |
my $x = any { $needle == $_ } @haystack; | |
} |
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
Benchmark: timing 10000000 iterations of any_it, grep_it... | |
any_it: 34 wallclock secs (34.36 usr + 0.00 sys = 34.36 CPU) @ 291036.09/s (n=10000000) | |
grep_it: 231 wallclock secs (230.08 usr + 0.01 sys = 230.09 CPU) @ 43461.25/s (n=10000000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment