return \@array or @array

Abigail abigail at abigail.be
Thu Sep 12 20:14:15 BST 2013


On Thu, Sep 12, 2013 at 02:05:13PM -0300, Daniel de Oliveira Mantovani wrote:
> On 12 September 2013 13:05, Jérôme Étévé <jerome.eteve at gmail.com> wrote:
> > Great :)
> >
> > so now:
> >
> > use Devel::Peek;
> >
> > sub foo{
> >     my @foo = 0..2;
> >     # Dump A
> >     print Dump(\@foo);
> >     return @foo;
> > }
> >
> > my @foo = foo();
> >
> > # Dump B
> > print Dump(\@foo);
> >
> > Prints quite interesting resutls. It shows both references are the
> > same, with only the intermediate PVAV changing.
> >
> > I'm still not quite sure about the real benefit of return \@array though.
> 
> 
> Because you are dumb like a stone,

That doesn't seem to be called for.

> [admin at localhost ~]$ time perl -E 'sub f {@a=1..9999999;return \@a}@b=f();'
> 
> real    0m1.802s
> user    0m1.433s
> sys    0m0.364s
> [admin at localhost ~]$ time perl -E 'sub f {@a=1..9999999;return @a}@b=f();'
> 
> real    0m3.331s
> user    0m2.695s
> sys    0m0.621s


Uhm, now you're just measuring half of the given program. It's not just 
about returning something from a method, it's *also* about looping over
the elements. It turns out that returning a reference is still faster,
but your benchmark doesn't show that.



Abigail


More information about the london.pm mailing list