On 11/3/07, howa <[EMAIL PROTECTED]> wrote:

> Passing parameters (in or return) during function calls:
>
> Pass by reference can improve performance: Array (List) , Scalar
> Pass by reference can't improve performance: Hash, Object, Reference
>
> Is that true?

No.

Passing a hash or array variable by reference will generally be more
efficient than passing a large list of elements, because the reference
is a single item and lists may be arbitrarily large. (Also, the data
structure typically needs to be "rebuilt" after it's passed as a list,
costing extra overhead.)

When you're passing a single item, it's just the same as passing a
scalar (every reference is a scalar, and every object is passed as a
reference). So there's no advantage (and probably a disadvantage) to
introducing an extra layer of referencing.

But how are you going to use this information? Would you change this
first sub to work like the second one?

  &silly_1($fred, $barney, $betty, $wilma); # four parameters

  my $params = [$fred, $barney, $betty, $wilma];
  &silly_2($params); # one parameter

It looks promising. Is the second subroutine call more efficient than
the first one? Yes, it probably is. Does this style make the program
run any faster? Probably not; it takes about as long to build $params
as to call the sub with a list of parameters.

It turns out that writing your code in the ordinary way IS the
efficient way. So why not write your code in the clearest,
easiest-to-maintain style?

Its also worth pointing out that, unless the subroutine in question
uses very few processor cycles to do its work, the time taken to pass
and return parameters is probably dwarfed by the subroutine's own run
time and internal sub-calling overhead. There's no point in shaving a
few processor cycles off of the call itself if the execution takes
thousands or millions of them. It's like using a 200-MPH racing car to
do the grocery shopping. The race car is awfully fast, but it still
takes about half an hour to get the groceries home every week.
Performance is great, but getting the job done is the bottom line.

Having said that, if you really need to know performs better, the One
True Answer can always be found by writing the code more than one way
and benchmarking it.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to