Assigning map result, and in-place map

Bert van Leeuwen bert at e.co.za
Fri Sep 3 04:25:25 PDT 2010


Pelle Wrote:

> On 09/03/2010 12:32 PM, Bert van Leeuwen wrote:
> > Pelle Wrote:
> >
> >> On 09/03/2010 11:42 AM, Bert van Leeuwen wrote:
> >>
> >>> 2) Related to above, I want to do something like map, but not return a new array, I want to modify elements in-place in the array. How do I do that? (without explicitly iterating with foreach etc.)
> >>
> >> I don't know if this is intended to be supported, but at least for now
> >> this works:
> >>
> >>       int[] xs = [1,2,3,4,5,6,7];
> >>       copy(map!`a*a`(xs), xs);
> >>
> >>       writeln(xs);
> >>
> >> [1, 4, 9, 16, 25, 36, 49]
> >
> >
> > Interesting. For large arrays, that seems to take almost double the time of creating a new one with array() (as in Nick's reply). So copy() obviously doesn't do it in place (not surprising given its name), and presumably makes a temporary array first from which to copy to a.
> >
> 
> Try it again with -inline. For me it's within 10% with inline, and gains 
> even more if you add -O and -release.
> 
> It also doesn't allocate, which is just a bonus. :-)

Awesome, that made it run about 4x faster :) thanks!



More information about the Digitalmars-d mailing list