It doesn't matter. Your loop administration isn't the bottleneck,
the method call dominates the running time.
But how many parameters do you have anyway? If you look at the total
program, is the loop going to be the bottleneck? Optimizing something
that only takes a small part of the running time isn't very useful.
Here are some benchmarking results, which show that it's the method
call being the bottleneck.
Abigail
#!/usr/bin/perl
use strict;
use warnings 'all';
use Benchmark::Sized; # To be released...
use CGI;
use vars qw /$cgi/;
$cgi = CGI -> new;
my $stats = sized_timethese
run_for => 1,
start_size => 2,
end_size => 4096,
steps => 12,
seed => sub {
$main::cgi -> delete_all;
foreach (1 .. $_ [0]) {$main::cgi -> param ($_, $_)}
},
code => {
map1 =>
'my %input = ();
map {$input {$_} = $main::cgi -> param ($_)} $main::cgi -
+> param',
map2 =>
'my %input =
map {$_ => $main::cgi -> param ($_)} $main::cgi -> param',
grep =>
'my %input = ();
grep {$input {$_} = $main::cgi -> param ($_)} $main::cgi -
+> param',
for =>
'my %input = ();
$input {$_} = $main::cgi -> param ($_) for $main::cgi -> p
+aram'
}
;
print_timed $stats, type => 'sized';
__END__
Runs/second:
Size for grep map1 map2
2: 9569.16 9774.55 9773.64 9569.16
4: 5749.09 5749.09 5749.09 5688.57
8: 3200.00 3169.81 3169.81 3140.19
16: 1689.62 1659.26 1644.04 1644.04
32: 872.73 837.38 845.28 853.33
64: 439.45 426.67 425.71 430.77
128: 222.22 216.22 214.29 216.50
256: 110.19 106.67 106.73 105.71
512: 53.40 51.38 51.38 50.93
1024: 25.69 24.53 24.53 24.07
2048: 12.50 11.93 11.82 11.54
4096: 6.31 5.94 5.83 5.61