Skip to content

Instantly share code, notes, and snippets.

@kentfredric
Forked from Getty/reggrephash.pl
Created February 28, 2012 22:43
Show Gist options
  • Save kentfredric/1935789 to your computer and use it in GitHub Desktop.
Save kentfredric/1935789 to your computer and use it in GitHub Desktop.
Benchmarking Regexp vs. Grep vs. Hash for "finding"
#!/usr/bin/env perl
use Benchmark qw( cmpthese );
my @prein = (
qw( bar baz foo )
);
my @preout = ( qw( bar baz boo ) ) ;
my %in = (
bar => 1,
baz => 1,
foo => 1,
);
my %out = (
bar => 1,
baz => 1,
boo => 1,
);
my $preregin = qr/bar|baz|foo/;
my $preregout = qr/bar|baz|boo/;
cmpthese(
10_000_000,
{
'PreFor' => sub {
for ( @prein ) {
return 1 if $_ eq 'foo';
}
},
'!PreFor' => sub {
for ( @preout ){
return 1 if $_ eq 'foo';
}
},
'For' => sub {
for (qw( bar baz foo )) {
return 1 if $_ eq 'foo';
}
},
'!For' => sub {
for (qw( bar baz boo ) ){
return 1 if $_ eq 'foo';
}
},
'PreGrep' => sub {
grep { $_ eq 'foo' } @prein;
},
'!PreGrep' => sub {
grep { $_ eq 'foo' } @preout;
},
'Grep' => sub {
grep { $_ eq 'foo' } qw(bar baz foo);
},
'!Grep' => sub {
grep { $_ eq 'foo' } qw(bar baz boo);
},
'PreRegexp' => sub { 'foo' =~ $preregin },
'!PreRegexp' => sub { 'foo' =~ $preregout },
'Regexp' => sub { 'foo' =~ m/bar|baz|foo/ },
'!Regexp' => sub { 'foo' =~ m/bar|baz|boo/ },
'Hash' => sub { defined $in{foo} },
'!Hash' => sub { defined $out{foo} },
'BuildHash' => sub {
my %hash = (
bar => 1,
baz => 1,
foo => 1,
);
defined $hash{foo}
},
'!BuildHash' => sub {
my %hash = (
bar => 1,
baz => 1,
boo => 1,
);
defined $hash{foo}
},
}
);
__END__
Rate PreRegexp BuildHash !PreFor !BuildHash !For For PreFor Regexp !Grep Grep PreGrep !PreGrep !PreRegexp Hash !Hash !Regexp
PreRegexp 1503759/s -- -10% -10% -13% -18% -35% -37% -37% -42% -46% -46% -47% -49% -83% -88% -92%
BuildHash 1663894/s 11% -- -0% -4% -9% -28% -30% -30% -36% -40% -40% -41% -44% -82% -87% -91%
!PreFor 1669449/s 11% 0% -- -4% -9% -28% -30% -30% -36% -40% -40% -41% -44% -81% -87% -91%
!BuildHash 1730104/s 15% 4% 4% -- -5% -26% -27% -27% -33% -38% -38% -39% -42% -81% -86% -90%
!For 1824818/s 21% 10% 9% 5% -- -22% -23% -23% -30% -34% -35% -35% -38% -80% -85% -90%
For 2325581/s 55% 40% 39% 34% 27% -- -2% -2% -10% -17% -17% -17% -21% -74% -81% -87%
PreFor 2375297/s 58% 43% 42% 37% 30% 2% -- -0% -9% -15% -15% -16% -20% -74% -81% -87%
Regexp 2380952/s 58% 43% 43% 38% 30% 2% 0% -- -8% -15% -15% -15% -20% -74% -81% -87%
!Grep 2597403/s 73% 56% 56% 50% 42% 12% 9% 9% -- -7% -7% -8% -12% -71% -79% -85%
Grep 2785515/s 85% 67% 67% 61% 53% 20% 17% 17% 7% -- -0% -1% -6% -69% -78% -84%
PreGrep 2793296/s 86% 68% 67% 61% 53% 20% 18% 17% 8% 0% -- -1% -6% -69% -78% -84%
!PreGrep 2816901/s 87% 69% 69% 63% 54% 21% 19% 18% 8% 1% 1% -- -5% -69% -77% -84%
!PreRegexp 2958580/s 97% 78% 77% 71% 62% 27% 25% 24% 14% 6% 6% 5% -- -67% -76% -83%
Hash 9009009/s 499% 441% 440% 421% 394% 287% 279% 278% 247% 223% 223% 220% 205% -- -28% -50%
!Hash 12500000/s 731% 651% 649% 622% 585% 437% 426% 425% 381% 349% 347% 344% 322% 39% -- -30%
!Regexp 17857143/s 1087% 973% 970% 932% 879% 668% 652% 650% 587% 541% 539% 534% 504% 98% 43% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment