I think you're not understanding what references do. In your example, you are not comparing slinging a bunch of data around vs. using a reference, you are comparing directly accessing a lexical variable vs. de-referencing the same variable and then accessing. I would expect the de-ref to always be slower.

There are tons of reasons to use references, but if you want to see the speed advantages of them, bench something like this:

#!/usr/bin/perl -w use Benchmark; my @array; for (0..1000) { $array[$_] = rand(1000); } timethese(200, { 'ref' => sub { my @unsorted = @array; my $return_ref = sort_array_ref(\@unsorted); }, 'old skool' => sub { my @unsorted = @array; my @return_array = sort_array(@unsorted); }, }); sub sort_array_ref { my $array_ref = shift; my @sorted_array = sort @{$array_ref}; return \@sorted_array; } sub sort_array { my @array = @_; my @sorted_array = sort @array; return @sorted_array; } __END__ Benchmark: timing 200 iterations of old skool, ref... old skool: 6 wallclock secs ( 5.81 usr + 0.01 sys = 5.82 CPU) @ 34 +.36/s (n=200) ref: 6 wallclock secs ( 5.07 usr + 0.01 sys = 5.08 CPU) @ 39 +.37/s (n=200)

In reply to Re: Performance of Perl references by perrin
in thread Performance of Perl references by fuzzyping

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.