return \@array or @array

Abigail abigail at abigail.be
Thu Sep 12 15:44:42 BST 2013


On Thu, Sep 12, 2013 at 03:02:27PM +0100, Jérôme Étévé wrote:
> Hi all,
> 
> I reckon there's a popular belief going around that A is "faster" than B
> 
> sub fA{ ... ; return \@array; }
> sub fB{ ... ; return @array; }
> 
> foreach my $thing ( @{fa()} ){ ... }
> foreach my $thing ( fB() ){ ... }
> 
> My almost blind faith in the Perl internals gives me the gut feeling
> that as arrays are a very native Type in Perl, and the underlying AV
> holds a reference anyway (at the end of the day, it's C..), it
> shouldn't make much difference.
> 
> And that building a Perl reference of something that's already a C
> space reference isn't going to help much.
> 


The difference is that when using

   foreach my $thing (fB ()) {...}

you are iterating over *copies* of the elements of @array, where 
in the other variant, you're iterating over the original elements.

So, the first variant saves making a copy.



Abigail


More information about the london.pm mailing list