You seem to be confusing passing by reference with passing a reference. Perl always passes by reference, whether what you are passing is a reference or not.

On the other hand, a sub that starts with my (...) = @_; or similar effectively causes Perl to pass by value, even when you pass a reference.

The difference is not between passing by reference and passing by value. The difference in the size of the list being passed. When passing a reference to an array to a sub, only one argument is placed on the stack. When passing an array's contents to a sub, as many arguments are placed on the stack as there are elements in the array.

Seeing as the latter is done using a simple memcpy, performance should not be an issue. And it isn't. Using the reference is actually 9% slower for a typical array (N=10)!

Rate list ref list_nc list 132332/s -- -52% -56% ref 273067/s 106% -- -9% list_nc 300755/s 127% 10% --

Even for very large arrays, the performance of just passing the array's elements is quite good. Passing all 10,000 elements of an array is only 14% slower than passing the reference alone!

Rate list list_nc ref list 148/s -- -69% -73% list_nc 469/s 218% -- -14% ref 545/s 269% 16% --

Performance gains is obviously not the reason passing references is done. (Although perceived performance gains could be.)

(I included a trivial load to get more reasonable numbers.)

Benchmark code:

use strict; use warnings; use Benchmark qw( cmpthese ); sub pass_ref { my ($a) = @_; my $x = ''; $x .= $_ for @$a; 1; } sub pass_list { my @a = @_; my $x = ''; $x .= $_ for @a; 1; } sub pass_list_no_copy { my $x = ''; $x .= $_ for @_; 1; } my %tests = ( ref => 'pass_ref(\@a);', list => 'pass_list(@a);', list_nc => 'pass_list_no_copy(@a);', ); $_ = 'use strict; use warnings; our @a; ' . $_ for values(%tests); our @a = ('-') x ( $ARGV[0] || 10 ); cmpthese(-1, \%tests);

In reply to Re^2: Array or Hash Reference to Subroutine by ikegami
in thread How to use @EXPORT by pkperl

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.