Skip to content

Instantly share code, notes, and snippets.

@tobyink
Forked from ology/.pl
Last active August 4, 2023 14:47
Show Gist options
  • Save tobyink/9a786346c35b89a965034b95cf2096c9 to your computer and use it in GitHub Desktop.
Save tobyink/9a786346c35b89a965034b95cf2096c9 to your computer and use it in GitHub Desktop.
Benchmark any vs grep
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark;
use List::Util 'any';
use match::simple::XS;
use match::simple qw(match);
my $count = shift || 2_000_000;
my $needle = 111;
my @haystack = (0 .. 999);
timethese($count, {
grep_it => \&grep_it,
any_it => \&any_it,
match_it => \&match_it,
});
sub grep_it {
my $x = grep { $needle == $_ } @haystack;
}
sub any_it {
my $x = any { $needle == $_ } @haystack;
}
sub match_it {
my $x = match $needle, \@haystack;
}
Benchmark: timing 2000000 iterations of any_it, grep_it, match_it...
any_it: 4 wallclock secs ( 5.27 usr + 0.00 sys = 5.27 CPU) @ 379506.64/s (n=2000000)
grep_it: 36 wallclock secs (35.41 usr + 0.00 sys = 35.41 CPU) @ 56481.22/s (n=2000000)
match_it: 3 wallclock secs ( 2.95 usr + 0.00 sys = 2.95 CPU) @ 677966.10/s (n=2000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment