vol7ron> How expensive is it to dereference an array ref in Perl?
ikegami> You're doing more than just dereferencing an array.
vol7ron> But the question still stands
Again, this is a useless question. The alternative is never between simply dereferencing an array and something else.
But since you insist, it's 37 ns (37 billionths of a second) for me.
use Benchmark qw( cmpthese );
my %tests = (
deref => 'my @y = @$x;',
none => 'my @y = @x;',
);
$_ = 'use strict; use warnings; our $x; our @x; ' . $_
for values %tests;
{
local our @x = ();
local our $x = \@x;
cmpthese(-3, \%tests);
}
Result:
Rate deref none
deref 3187659/s -- -12%
none 3616848/s 13% --
Time taken by each deref = 1/3187659 s - 1/3616848 s = 37 ns
It's tiny! Dereferencing the array only accounts for 12% of the time taken to dereference an empty array and copying it into another!
Does the operation first generate a list via split (1) and create an array ref (2), then copy the values of the array ref into a new array when dereferencing (3)?
Yes,
split
returns a list. Except in scalar context.[ ... ]
doesn't just create a reference, it also creates an array and copies the values into it.No, dereferencing doesn't copy values.
Does it morph the current arrayref in place?
It would be really bad if a reference turned into something else. What do you actually mean?