in reply to Re: hash interface
in thread hash interface
However, it is safer and a recommended practice to pass named arguments as a hash reference
Depends. If you already have references to pass around, ok. If your subroutine / method takes mixed type arguments, ok. But if it is ever only being passed a hash, then constructing a hash at every sub/method call is costly and overkill.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: hash interface
by GrandFather (Saint) on Mar 06, 2009 at 10:47 UTC | |
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:
Prints:
True laziness is hard work | [reply] [d/l] [select] |
by shmem (Chancellor) on Mar 06, 2009 at 11:14 UTC | |
Your benchmarks support my point. But there are two subs missing - assignment of a passed hash reference to a scalar and to a hash:
which yields
which shows that constructing a hash reference from the argument list and actually using it is always slowest; passing a plain list is fastest. | [reply] [d/l] [select] |