#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # use fixed values for repeatable tests my @input = 1..100; my @valuesToMatch = map {$_*14} 1..10; my @expectedOutput = (14,28,42,56,70,84,98); print Dumper(\@valuesToMatch); # precompute what can be precomputed # for the regex approach my $regex_str = join '|', map {quotemeta} sort { length $b <=> length $a } @valuesToMatch; my $regex = qr/(?:\A|\0)($regex_str)(?:\z|\0)/; # for the hash approach my %matchtbl = map {$_=>1} @valuesToMatch; # the different implementations, in a dispatch table my %subs = ( hash => sub { my @matches = grep {$matchtbl{$_}} @input; return @matches; }, regex => sub { my @matches = join("\0", @input) =~ /$regex/g; return @matches; }, grep => sub { my @matches; for my $in (@input) { push @matches, $in if grep {$in==$_} @valuesToMatch; } return @matches; }, ); # check to make sure our implementations are all working use Test::More; for my $sub (sort keys %subs) { my @results = $subs{$sub}->(); is_deeply \@results, \@expectedOutput, $sub or diag explain \@results; } done_testing; # now benchmark the implementations use Benchmark qw/cmpthese/; cmpthese(-3, \%subs); #### $VAR1 = [ 14, 28, 42, 56, 70, 84, 98, 112, 126, 140 ]; ok 1 - grep ok 2 - hash ok 3 - regex 1..3 Rate grep regex hash grep 33107/s -- -49% -85% regex 65160/s 97% -- -70% hash 218506/s 560% 235% --