use strict; use warnings; my @numbers = (1..100); my %count; $count{$_}++ foreach @numbers; print "$_ occurs $count{$_} time(s)\n" foreach sort { $a <=> $b } keys %count; #### 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, } ); #### Rate hash array hash 19.4/s -- -55% array 43.2/s 122% --