The difference is the amount of memory allocated and the way that memory is structured.

When passing a list of integers, they are just pushed onto a stack as a series of pointers to small (probably SvIVs), discrete chunks of memory.

When passing a reference to an array, that array has to be constructed first. And an array consists of single, contiguous lump of ramp containing pointers to SVs that contain pointers to the SvIVs. For large arrays, allocating that large contiguous chunk of memory will mean a call out to the OS for a new virtual allocation. And that costs a lot relative to (re)allocating small chunks from the existing memory pool. It then has to allocate the memory for the individual elements and write their pointers to the contiguous chunk, much as it does when pushing the list onto a stack. So the extra step is all cost.

The savings of passing a reference to an array comes when that array already exists. Then just a pointer is pushed rather than pointers to all the contained elements.

Note: My description may not be completely accurate, see PerlGuts Illustrated for the real details.

For the difference in numbers::

sub x{}; cmpthese -1,{ a=>q[ x( 1..1e6 ) ], b=>q[x( [1..1e6] ) ] };; Rate b a b 10.5/s -- -56% a 23.8/s 126% -- @a = 1 .. 1e6;; cmpthese -1,{ a=>q[ x( @a ) ], b=>q[x( \@a ) ] };; Rate a b a 24.1/s -- -100% b 2903218/s 12025922% --

In the first case, the 44% penalty of b is the time taken to allocate that big, contiguous chunk.

In the latter, b's huge advantage is pushing 1 reference .v. pushing 1 million references.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re^2: Curious Perl Behavior... by BrowserUk
in thread Curious Perl Behavior... by ack

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.