return \@array or @array

Daniel de Oliveira Mantovani daniel.oliveira.mantovani at gmail.com
Thu Sep 12 20:23:40 BST 2013


On 12 September 2013 16:14, Abigail <abigail at abigail.be> wrote:
> 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.

My benchmark does show that. Look again.

>
>
>
> Abigail



-- 

-dom

--

Daniel de Oliveira Mantovani
Business Analytic Specialist
Perl Evangelist /Astrophysics hobbyist.
+55 11 9 8538-9897
XOXO



More information about the london.pm mailing list