Here's a way to do the transfer without using map:
On the other hand, I did a quick benchmark, which ended up showing little difference in efficiency between the solutions:my @keys = grep /^t/, keys %hash; my %newhash; @newhash{@keys} = @hash{@keys};
So I guess you can just go with whichever solution you like best. :)#!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) @ 40 +34.15/s (n=21381) grep_slice: 6 wallclock secs ( 5.28 usr + 0.00 sys = 5.28 CPU) @ 40 +47.73/s (n=21372) just_map: 6 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 39 +90.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) @ 41 +2.17/s (n=2168) grep_slice: 6 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU) @ 41 +2.95/s (n=2168) just_map: 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 39 +8.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% --
In reply to Re: Miniature golf question
by chipmunk
in thread Miniature golf question
by nysus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |