When b is an array there will be a reallocation when doing *= or /=. Instead you can use scale!(c,2) for *=. For + you can use broadcast!(+,A,[2]). Maybe there should be a broadcast(Function,Array,Number) method.
If you update julia you'll also notice that A+=2 throws a deprecation warning because in the future you'll have to use .+ and .- for broadcasting. *= ad /= will still work for matrices. 2014-03-10 13:24 GMT+01:00 Carlos Becker <[email protected]>: > > 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. -- Med venlig hilsen Andreas Noack Jensen
