use strict; use warnings; use Benchmark qw(cmpthese); @main::numbers = map {int(rand(10000))+1 } 1 .. 300000; sub hash { my %count; $count{$_}++ foreach @main::numbers; my @results = map { [ $_ => $count{$_} ] } sort { $a <=> $b } keys %count; } sub array { my @count; $count[$_]++ foreach @main::numbers; my @results = map { [ $_ => $count[$_] ] } grep { $count[$_] } 0 .. $#count; } cmpthese ( -5, { hash => \&hash, array => \&array, } );