develooper Front page | perl.perl5.porters | Postings from April 2008

Re: Make built-in list functions continuous

Thread Previous
wren argetlahm
April 2, 2008 03:02
Re: Make built-in list functions continuous
Message ID:
Graham Barr wrote:
> On Apr 1, 2008, at 8:49 AM, Nicholas Clark wrote:
> > >     @xs = map { f($_) } @ys;
> > >
> > > In that code, `map` will construct an array in memory, but when it
> > > returns it, the array will be copied cell by cell into a new array
> > > constructed as @xs. We can optimize this to remove that intermediate
> > > array by using the following code instead:
> > >
> > >     @xs = ();
> > >     push @xs, f($_) foreach @ys;
> I am not convinced that is always "better" performance. It depends what 
> you metric is. While it may use less memory, it is more OPs to run and 
> may result in slower runtime performance in some cases.

In all my benchmarking I was only looking at the runtime. By that metric 
performance is much better (+60~+100%) for large lists, and approaching 
equivalent as the size of lists decreases; which is consistent with 
asymptotic differences due to building intermediate values, garbage 
collecting them, and generally wrecking the cache.

Even at the low end of the spectrum I didn't notice the `map` version 
being even marginally faster. I'll grant there may be some corner cases 
where that happens, but I didn't find them.

Live well,

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About