The cost depends a great deal on what you do with the argument list inside the sub/method. If you don't copy the argument list in some fashion then the hash construction can be expensive for more than a few parameters. However if you would otherwise copy the argument list into an array or hash inside the sub, the cost of constructing the hash is much less relevant. Consider:

use strict; use warnings; use Benchmark qw(cmpthese); for my $argCount (2, 10, 100) { my @args = (1 .. $argCount); print "\nFor $argCount elements:\n"; cmpthese (-1, { hashArg => sub {noCopy ({@args})}, listArg => sub {noCopy (@args)}, toList => sub {toList (@args)}, toHash => sub {toHash (@args)}, } ); } sub noCopy { } sub toList { my @args = @_; } sub toHash { my %hash = @_; }

Prints:

For 2 elements: Rate hashArg toList toHash listArg hashArg 1066864/s -- -12% -27% -74% toList 1205941/s 13% -- -17% -71% toHash 1459027/s 37% 21% -- -64% listArg 4106796/s 285% 241% 181% -- For 10 elements: Rate toList hashArg toHash listArg toList 344656/s -- -14% -17% -91% hashArg 399964/s 16% -- -4% -90% toHash 416260/s 21% 4% -- -89% listArg 3846573/s 1016% 862% 824% -- For 100 elements: Rate hashArg toHash toList listArg hashArg 38850/s -- -6% -7% -98% toHash 41541/s 7% -- -1% -98% toList 41895/s 8% 1% -- -98% listArg 1686904/s 4242% 3961% 3926% --

True laziness is hard work

In reply to Re^3: hash interface by GrandFather
in thread hash interface by baxy77bax

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.