use Benchmark; my %hash; my ($c, $i) = 1000, 100; for (1 .. $c) { $hash{"test$_"} = $_; $hash{"foo$_"} = $_; } sub SUBSTR { foreach my $bid (sort { substr ($a,4) <=> substr ($b,4) } grep {/^test(\d+)$/} keys %hash) { } } sub SCHWARTZ { for my $bid (map { $_->[1] } sort { $a->[0] <=> $b->[0] } map { m/^test(\d+)$/ ? [$1, $_] : () } keys %hash) { } } timethese($i, { SUBSTR => \&SUBSTR, SCHWARTZ => \&SCHWARTZ });