% cat 986735.pl #!/usr/bin/env perl use Modern::Perl; use Benchmark qw(:all); my @words = split /\s+/, `cat bigfile`; #8.5MB file say scalar @words, " words in bigfile"; #1.3M words my $match = 'professional'; # appears 16 times scattered through bigfile cmpthese( 10, { 'hash it' => \&hashit, 'grep it' => \&grepit, 'first it' => \&firstit, }); sub hashit { my %h; @h{@words} = (); my $exists = exists $h{$match}; } sub grepit { my $exists = grep { $_ eq $match } @words; } sub firstit { use List::Util 'first'; my $exists = first { $_ eq $match } @words; } % perl 986735.pl 1293687 words in bigfile Rate hash it grep it first it hash it 3.33/s -- -43% -89% grep it 5.80/s 74% -- -81% first it 29.8/s 795% 413% --