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.
In reply to Re^2: Curious Perl Behavior...
by BrowserUk
in thread Curious Perl Behavior...
by ack
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |