use strict; use warnings; use Benchmark; use List::Util qw(shuffle); my $count=5000000; my @existing_indexes = ( 5,6,8..53,62..106); my @indexes = (0..200); my @shuffled_indexes = shuffle @indexes; my %test_hash; my @test_array; @test_array[ @existing_indexes ] = (); @test_hash{ @existing_indexes } = (); Benchmark::cmpthese($count, { arrayLookup => q{ my $val = &arrayLookup( \@test_array, \@indexes ); }, hashLookup => q{ my $val = &hashLookup( \%test_hash, \@indexes ); }, shuffled_arrayLookup => q{ my $val = &arrayLookup( \@test_array, \@shuffled_indexes ); }, shuffled_hashLookup => q{ my $val = &hashLookup( \%test_hash, \@shuffled_indexes ) }, } ); sub arrayLookup { my $array = shift; my $indexes = shift; my @results = map { exists $array->[$_]; } @$indexes; return \@results; } sub hashLookup { my $hash = shift; my $keys = shift; my @results = map { exists $hash->{$_}; } @$keys; return \@results; } #### Run 1: Rate shuffled_arrayLookup hashLookup shuffled_hashLookup arrayLookup shuffled_arrayLookup 409199/s -- -1% -1% -2% hashLookup 411286/s 1% -- -1% -2% shuffled_hashLookup 415076/s 1% 1% -- -1% arrayLookup 417781/s 2% 2% 1% -- Run 2: shuffled_arrayLookup arrayLookup shuffled_hashLookup hashLookup 412371/s -- -0% -1% -1% shuffled_arrayLookup 413976/s 0% -- -0% -1% arrayLookup 414491/s 1% 0% -- -1% shuffled_hashLookup 416667/s 1% 1% 1% -- C:\Documents and Settings\Mark\Desktop\Downloads> exists.pl Rate shuffled_arrayLookup hashLookup shuffled_hashLookup arrayLookup shuffled_arrayLookup 409199/s -- -1% -1% -2% hashLookup 411286/s 1% -- -1% -2% shuffled_hashLookup 415076/s 1% 1% -- -1% arrayLookup 417781/s 2% 2% 1% -- Run 3: Rate hashLookup shuffled_arrayLookup arrayLookup shuffled_hashLookup hashLookup 412371/s -- -0% -1% -1% shuffled_arrayLookup 413976/s 0% -- -0% -1% arrayLookup 414491/s 1% 0% -- -1% shuffled_hashLookup 416667/s 1% 1% 1% --