my @keys = grep /^t/, keys %hash; my %newhash; @newhash{@keys} = @hash{@keys}; #### #!perl -w use strict; use Benchmark; Benchmark->import(qw/cmpthese/) if $^V; my $time = shift || 5; my $size = shift || 100; my %hash; for (0 .. $size) { if ($_ < $size / 4) { $hash{"t$_"} = 1; } else { $hash{"a$_"} = 1; } } my %bms = ( grep_map => sub { my @keys = grep /^\t/, keys %hash; my %newhash = map { ($_, $hash{$_}) } @keys; }, just_map => sub { my %newhash = map /^\t/ ? ( $_, $hash{$_} ) : (), keys %hash; }, grep_slice => sub { my @keys = grep /^\t/, keys %hash; my %newhash; @newhash{@keys} = @hash{@keys}; }, ); if ($^V) { cmpthese(-$time, \%bms); } else { timethese(-$time, \%bms); } __END__ % perl bm.pl Benchmark: running grep_map, grep_slice, just_map, each for at least 5 CPU seconds... grep_map: 6 wallclock secs ( 5.29 usr + 0.01 sys = 5.30 CPU) @ 4034.15/s (n=21381) grep_slice: 6 wallclock secs ( 5.28 usr + 0.00 sys = 5.28 CPU) @ 4047.73/s (n=21372) just_map: 6 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 3990.06/s (n=20868) Rate just_map grep_map grep_slice just_map 3990/s -- -1% -1% grep_map 4034/s 1% -- -0% % perl/bin/perl bm.pl 5 1000 Benchmark: running grep_map, grep_slice, just_map, each for at least 5 CPU seconds... grep_map: 6 wallclock secs ( 5.26 usr + 0.00 sys = 5.26 CPU) @ 412.17/s (n=2168) grep_slice: 6 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU) @ 412.95/s (n=2168) just_map: 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 398.87/s (n=2110) Rate just_map grep_map grep_slice just_map 399/s -- -3% -3% grep_map 412/s 3% -- -0% grep_slice 413/s 4% 0% --