Re: return \@array or @array
On 12 Sep 2013, at 20:23, Daniel de Oliveira Mantovani wrote: > My benchmark does show that. Look again. Wrong. Copy-on-write semantics. Aside from the fact you're being odious in public again, arguing with Abigail is generally not likely to result in you being proven right. James
Re: return \@array or @array
On Thu, Sep 12, 2013 at 04:23:40PM -0300, Daniel de Oliveira Mantovani wrote: > On 12 September 2013 16:14, Abigail 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é 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@localhost ~]$ time perl -E 'sub f {@a=1..999;return \@a}@b=f();' > >> > >> real0m1.802s > >> user0m1.433s > >> sys0m0.364s > >> [admin@localhost ~]$ time perl -E 'sub f {@a=1..999;return @a}@b=f();' > >> > >> real0m3.331s > >> user0m2.695s > >> sys0m0.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. > Can you point me out where in sub f {@a=1..999;return \@a}@b=f(); you are looping over the numbers 1 to 999? Abigail
Re: return \@array or @array
Abigail, the point was just show the reference performance. The point is, Jérôme Étévé said: "I'm still not quite sure about the real benefit of return \@array though." And my answer was just for it. On 12 September 2013 17:04, Abigail wrote: > On Thu, Sep 12, 2013 at 04:23:40PM -0300, Daniel de Oliveira Mantovani wrote: >> On 12 September 2013 16:14, Abigail 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é 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@localhost ~]$ time perl -E 'sub f {@a=1..999;return >> >> \@a}@b=f();' >> >> >> >> real0m1.802s >> >> user0m1.433s >> >> sys0m0.364s >> >> [admin@localhost ~]$ time perl -E 'sub f {@a=1..999;return @a}@b=f();' >> >> >> >> real0m3.331s >> >> user0m2.695s >> >> sys0m0.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. >> > > > Can you point me out where in > > sub f {@a=1..999;return \@a}@b=f(); > > you are looping over the numbers 1 to 999? > > > > Abigail -- -dom -- Daniel de Oliveira Mantovani Business Analytic Specialist Perl Evangelist /Astrophysics hobbyist. +55 11 9 8538-9897 XOXO
Re: return \@array or @array
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é 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@localhost ~]$ time perl -E 'sub f {@a=1..999;return \@a}@b=f();' > > real0m1.802s > user0m1.433s > sys0m0.364s > [admin@localhost ~]$ time perl -E 'sub f {@a=1..999;return @a}@b=f();' > > real0m3.331s > user0m2.695s > sys0m0.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
Re: return \@array or @array
On 12 September 2013 16:14, Abigail 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é 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@localhost ~]$ time perl -E 'sub f {@a=1..999;return \@a}@b=f();' >> >> real0m1.802s >> user0m1.433s >> sys0m0.364s >> [admin@localhost ~]$ time perl -E 'sub f {@a=1..999;return @a}@b=f();' >> >> real0m3.331s >> user0m2.695s >> sys0m0.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
Re: return \@array or @array
On 12 September 2013 13:05, Jérôme Étévé 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, [admin@localhost ~]$ time perl -E 'sub f {@a=1..999;return \@a}@b=f();' real0m1.802s user0m1.433s sys0m0.364s [admin@localhost ~]$ time perl -E 'sub f {@a=1..999;return @a}@b=f();' real0m3.331s user0m2.695s sys0m0.621s > > Cheers, > > J. > > > > On 12 September 2013 16:32, Yitzchak Scott-Thoennes > wrote: >> On Thu, Sep 12, 2013 at 7:33 AM, Jérôme Étévé wrote: >>> use Devel::Peek; >>> >>> my $foo_ref; >>> >>> sub foo{ >>> my @foo = 0..2; >>> $foo_ref = \@foo; >>> return @foo; >>> } >>> >>> print Dump([ foo() ]); >>> print Dump($foo_ref); >>> >>> Shows that apparently @foo is deeply copied when it's returned. Is that >>> correct? >> >> No. But perl notices when you leave foo() that @foo has an external >> reference, so it allocates a new one in preparation for the next call >> to foo(). >> >>> I'm not an expert in perl guts, but it seems there's no such thing as >>> a 'list' native structure. They're both PVAV. >> >> The return of a sub isn't a native structure, it's just a range of >> elements on a stack. (So basically a shallow copy, but without an >> array container.) There is no mechanism for returning a PVAV, just a >> reference to one. >> > > > > -- > Jerome Eteve > +44(0)7738864546 > http://www.eteve.net/ > -- -dom -- Daniel de Oliveira Mantovani Business Analytic Specialist Perl Evangelist /Astrophysics hobbyist. +55 11 9 8538-9897 XOXO
Re: return \@array or @array
On Thu, Sep 12, 2013 at 5:05 PM, Jérôme Étévé wrote: > > I'm still not quite sure about the real benefit of return \@array though. There isn't one. In fact, there are downsides to using it if you don't need it, like accidentally modifying in two places where you only wanted to modify in one. Python passes by reference by default and I see a lot of code that makes a shallow copy to avoid such bugs. So passing by value helps eliminate a whole category of programming bug. In any case, it's an interpreted programming language. Write bits that have greater performance requirements in XS. Or don't write them in perl at all. James
Re: return \@array or @array
On Thu, Sep 12, 2013 at 7:33 AM, Jérôme Étévé wrote: > use Devel::Peek; > > my $foo_ref; > > sub foo{ > my @foo = 0..2; > $foo_ref = \@foo; > return @foo; > } > > print Dump([ foo() ]); > print Dump($foo_ref); > > Shows that apparently @foo is deeply copied when it's returned. Is that > correct? No. But perl notices when you leave foo() that @foo has an external reference, so it allocates a new one in preparation for the next call to foo(). > I'm not an expert in perl guts, but it seems there's no such thing as > a 'list' native structure. They're both PVAV. The return of a sub isn't a native structure, it's just a range of elements on a stack. (So basically a shallow copy, but without an array container.) There is no mechanism for returning a PVAV, just a reference to one.
Re: return \@array or @array
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. Cheers, J. On 12 September 2013 16:32, Yitzchak Scott-Thoennes wrote: > On Thu, Sep 12, 2013 at 7:33 AM, Jérôme Étévé wrote: >> use Devel::Peek; >> >> my $foo_ref; >> >> sub foo{ >> my @foo = 0..2; >> $foo_ref = \@foo; >> return @foo; >> } >> >> print Dump([ foo() ]); >> print Dump($foo_ref); >> >> Shows that apparently @foo is deeply copied when it's returned. Is that >> correct? > > No. But perl notices when you leave foo() that @foo has an external > reference, so it allocates a new one in preparation for the next call > to foo(). > >> I'm not an expert in perl guts, but it seems there's no such thing as >> a 'list' native structure. They're both PVAV. > > The return of a sub isn't a native structure, it's just a range of > elements on a stack. (So basically a shallow copy, but without an > array container.) There is no mechanism for returning a PVAV, just a > reference to one. > -- Jerome Eteve +44(0)7738864546 http://www.eteve.net/
Re: return \@array or @array
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
Re: return \@array or @array
use Devel::Peek; my $foo_ref; sub foo{ my @foo = 0..2; $foo_ref = \@foo; return @foo; } print Dump([ foo() ]); print Dump($foo_ref); Shows that apparently @foo is deeply copied when it's returned. Is that correct? I'm not an expert in perl guts, but it seems there's no such thing as a 'list' native structure. They're both PVAV. J. On 12 September 2013 15:08, Joel Bernstein wrote: > You're wrong. Where you're going wrong is assuming that "return @foo" is > going to "return an array". It returns a list of values, the same list that > the array held in the subroutine scope. > > That is: > > sub foo { > my @foo = 1..100; > return @foo; > } > > sub bar { > my @bar = 1..100; > return \@bar; > } > > foo() will return a list of 99 elements. > bar() will return a list of 1 element, which is a reference to an array > containing 99 elements. > > Does that make more sense? > > /joel > > > On 12 September 2013 16:02, 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. >> >> Any insight? >> >> Cheers, >> >> Jerome. >> >> -- >> Jerome Eteve >> +44(0)7738864546 >> http://www.eteve.net/ >> > -- Jerome Eteve +44(0)7738864546 http://www.eteve.net/
Re: return \@array or @array
The below script pretty reliably puts func1 slower than func2, albeit by only about a second on a pretty beefy machine. use strict; use Time::HiRes qw( time ); sub func1 { my @array = (1..4000); return @array; } sub func2 { my @array = (1..4000); return \@array; } my $a = 0; my $first = time; foreach my $foo (@{func2()}) { $a++; } my $second = time; $a = 0; my $third = time; foreach my $foo (func1) { $a++; } my $fourth = time; print $second - $first . "\n"; print $fourth - $third . "\n"; On 12 September 2013 15:08, Joel Bernstein wrote: > You're wrong. Where you're going wrong is assuming that "return @foo" is > going to "return an array". It returns a list of values, the same list that > the array held in the subroutine scope. > > That is: > > sub foo { > my @foo = 1..100; > return @foo; > } > > sub bar { > my @bar = 1..100; > return \@bar; > } > > foo() will return a list of 99 elements. > bar() will return a list of 1 element, which is a reference to an array > containing 99 elements. > > Does that make more sense? > > /joel > > > On 12 September 2013 16:02, 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. > > > > Any insight? > > > > Cheers, > > > > Jerome. > > > > -- > > Jerome Eteve > > +44(0)7738864546 > > http://www.eteve.net/ > > > > >
Re: return \@array or @array
Just my little test ;-) On 12 September 2013 16:28, Jasper wrote: > Shurely 100 elements :) > > > On 12 September 2013 15:08, Joel Bernstein wrote: > > > You're wrong. Where you're going wrong is assuming that "return @foo" is > > going to "return an array". It returns a list of values, the same list > that > > the array held in the subroutine scope. > > > > That is: > > > > sub foo { > > my @foo = 1..100; > > return @foo; > > } > > > > sub bar { > > my @bar = 1..100; > > return \@bar; > > } > > > > foo() will return a list of 99 elements. > > bar() will return a list of 1 element, which is a reference to an array > > containing 99 elements. > > > > Does that make more sense? > > > > /joel > > > > > > On 12 September 2013 16:02, 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. > > > > > > Any insight? > > > > > > Cheers, > > > > > > Jerome. > > > > > > -- > > > Jerome Eteve > > > +44(0)7738864546 > > > http://www.eteve.net/ > > > > > > > > > > > > -- > Jasper > >
Re: return \@array or @array
Shurely 100 elements :) On 12 September 2013 15:08, Joel Bernstein wrote: > You're wrong. Where you're going wrong is assuming that "return @foo" is > going to "return an array". It returns a list of values, the same list that > the array held in the subroutine scope. > > That is: > > sub foo { > my @foo = 1..100; > return @foo; > } > > sub bar { > my @bar = 1..100; > return \@bar; > } > > foo() will return a list of 99 elements. > bar() will return a list of 1 element, which is a reference to an array > containing 99 elements. > > Does that make more sense? > > /joel > > > On 12 September 2013 16:02, 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. > > > > Any insight? > > > > Cheers, > > > > Jerome. > > > > -- > > Jerome Eteve > > +44(0)7738864546 > > http://www.eteve.net/ > > > > > -- Jasper
Re: return \@array or @array
You're wrong. Where you're going wrong is assuming that "return @foo" is going to "return an array". It returns a list of values, the same list that the array held in the subroutine scope. That is: sub foo { my @foo = 1..100; return @foo; } sub bar { my @bar = 1..100; return \@bar; } foo() will return a list of 99 elements. bar() will return a list of 1 element, which is a reference to an array containing 99 elements. Does that make more sense? /joel On 12 September 2013 16:02, 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. > > Any insight? > > Cheers, > > Jerome. > > -- > Jerome Eteve > +44(0)7738864546 > http://www.eteve.net/ > >
return \@array or @array
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. Any insight? Cheers, Jerome. -- Jerome Eteve +44(0)7738864546 http://www.eteve.net/
Re: London.pm Dim Sum, Leong's Legends, Thursday 12th September 13:00
On Wed, 11 Sep 2013 06:52:31 +0100 James Laver wrote: > Motivated by the dual reasons of Jay Rayner having finally discovered > their xiao long bao and writing about them in tones that made me > crave them from when I used to do them regularly and a strong desire > to avoid a particular client meeting I don't want to be in, let's > have dim sum tomorrow - it's been ages! > > (Opposite De Hems) > Leong's Legends, > 4 Macclesfield st > W1D 6AX > > James I'm planning on turning up.. Though I guess coming from Twickenham I ought to be departing about now... :) -- Paul "LeoNerd" Evans leon...@leonerd.org.uk ICQ# 4135350 | Registered Linux# 179460 http://www.leonerd.org.uk/ signature.asc Description: PGP signature