$ perl 939870.pl 1000 Building arrays of 1000 with unique 4-char keys... done. Arrays have 1000 items each. Benchmarking... Rate arrays plus regex using a hash arrays plus regex 2.70/s -- -100% using a hash 1000000000000000/s 37000000000000000% -- $ perl 939870.pl 5000 Building arrays of 5000 with unique 4-char keys... done. Arrays have 5000 items each. Benchmarking... s/iter arrays plus regex using a hash arrays plus regex 9.40 -- -100% using a hash 1.00e-02 93900% -- $ perl 939870.pl 10000 Building arrays of 10000 with unique 4-char keys... done. Arrays have 10000 items each. Benchmarking... s/iter arrays plus regex using a hash arrays plus regex 46.3 -- -100% using a hash 3.00e-02 154267% -- $ perl 939870.pl 100000 Building arrays of 100000 with unique 4-char keys... done. Arrays have 100000 items each. Benchmarking... s/iter arrays plus regex using a hash arrays plus regex 4110 -- -100% using a hash 0.330 1245345% -- #### $ cat 939870.pl #!/usr/bin/perl use Modern::Perl; use Benchmark qw(:all); # create two lists, with the formats: # list1: (four characters A-Z) # RDAE # JFCS # # list2: (7 digit number, space, four chars A-Z) # 1234567 JIQZ # 2345678 EFOP my( @list1, @list2); my( %keys1, %keys2); my $array_size = $ARGV[0] || 100_000; print "Building arrays of $array_size with unique 4-char keys... "; for (1..$array_size){ my($s1, $s2) = ('',''); $s1 .= ('A'..'Z')[rand 26] for (1..4); $s2 .= ('A'..'Z')[rand 26] for (1..4); next if $keys1{$s1} or $keys2{$s2}; # make keys unique push @list1, $s1; # save keys in lists push @list2, int(rand(8999999)+1_000_000). " $s2"; } say "done.\nArrays have ". scalar(@list1). " items each. Benchmarking..."; cmpthese(1, { 'arrays plus regex' => \&array_plus_regex, 'using a hash' => \&using_hash, }); sub array_plus_regex { my @results; FOUND: for my $l1 (@list1){ for my $l2 (@list2){ if($l2 =~ / $l1/){ push @results, "$l1 ---> $`"; next FOUND; } } push @results, "$l1 ---> null"; } } sub using_hash { my @results; my %match; my $count = 0; for my $l2 (@list2){ my($k, $v) = split ' ', $l2; $match{$k} = $v; } for my $l1 (@list1){ if($match{$l1}){ push @results, "$l1 ---> $match{$l1}"; } else { push @results, "$l1 ---> null"; } } }