I'm heading out the door and don't have time to search for previous 
discussions, but this has come up many times in the past---there are good (if 
admittedly unfortunate) reasons for this. Someone needs to add this to the 
FAQ!

--Tim

On Monday, March 10, 2014 05:24:20 AM Carlos Becker wrote:
> Hello, I recently found out this:
> 
> julia>  b = rand((1024,1024))
> 
> julia>  sizeof(b)
> 8388608
> 
> julia>  @time b += 2;
> elapsed time: 0.007002567 seconds (8388704 bytes allocated)
> 
> 
> which, with further investigation, I could verify that it is allocating a
> new array every time I do something like b += 2,
> instead of modifying the original object b points to.
> 
> Is this an intended behavior, or better, is there a command to optimize
> such operations?
> I tried @devec but it also seems to generate a new destination array. I
> could also write my own function,
> but I suppose there are better ways to deal with this.
> 
> Code such as b ./= 2 is very common, and this introduces array duplication,
> increasing memory usage as well as
> additional unneeded operations (the former crashed my machine a few hours
> ago).
> 
> Thanks.

Reply via email to