Skip to content

Commit 1686280

Browse files
authored
Update advice on working with mutable arrays (#1091)
1 parent 69cc09c commit 1686280

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

docs/src/pages/api.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ end
305305
so long as the `Type` of the rebound variable (`x`, above) does not change.
306306

307307
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
309309
be *allocated* and later *garbage collected*, and for small, fixed-size arrays
310310
this can be a leading contribution to the cost. In the above code, a new array
311311
will be instantiated and allocated on each iteration of the loop. In order to
@@ -315,17 +315,12 @@ apply mutating functions to it:
315315
function average_position(positions::Vector{SVector{3,Float64}})
316316
x = zeros(MVector{3,Float64})
317317
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
320319
end
321-
x .= (/).(x, length(positions))
320+
x ./= length(positions)
322321
return x
323322
end
324323
```
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.
329324

330325
The functions `setindex`, `push`, `pop`, `pushfirst`, `popfirst`, `insert` and `deleteat`
331326
are provided for performing certain specific operations on static arrays, in

0 commit comments

Comments
 (0)