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

In reply to Re: map, grep, for, foreach by Abigail-II
in thread map, grep, for, foreach by mt2k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.