305
305
so long as the ` Type ` of the rebound variable (` x ` , above) does not change.
306
306
307
307
On the other hand, the above code for mutable containers like ` Array ` , ` MArray `
308
- or ` SizedArray ` is * not* very efficient. Mutable containers in Julia 0.5 must
308
+ or ` SizedArray ` is * not* very efficient. Mutable containers must
309
309
be * allocated* and later * garbage collected* , and for small, fixed-size arrays
310
310
this can be a leading contribution to the cost. In the above code, a new array
311
311
will be instantiated and allocated on each iteration of the loop. In order to
@@ -315,17 +315,12 @@ apply mutating functions to it:
315
315
function average_position (positions:: Vector{SVector{3,Float64}} )
316
316
x = zeros (MVector{3 ,Float64})
317
317
for pos ∈ positions
318
- # Take advantage of Julia 0.5 broadcast fusion
319
- x .= (+ ). (x, pos) # same as broadcast!(+, x, x, positions[i])
318
+ x .+ = pos
320
319
end
321
- x .= ( / ) . (x, length (positions) )
320
+ x ./= length (positions)
322
321
return x
323
322
end
324
323
```
325
- Keep in mind that Julia 0.5 does not fuse calls to ` .+ ` , etc (or ` .+= ` etc),
326
- however the ` .= ` and ` (+).() ` syntaxes are fused into a single, efficient call
327
- to ` broadcast! ` . The simpler syntax ` x .+= pos ` is expected to be non-allocating
328
- (and therefore faster) in Julia 0.6.
329
324
330
325
The functions ` setindex ` , ` push ` , ` pop ` , ` pushfirst ` , ` popfirst ` , ` insert ` and ` deleteat `
331
326
are provided for performing certain specific operations on static arrays, in
0 commit comments