|
|
|
|
|
by ffriend
3220 days ago
|
|
Even better would be to write (see dot vectorization [1]): C .= A .+ B
Benchmarks for 3 matrices of size 1000x1000: julia> using BenchmarkTools
julia> @benchmark C = A + B
BenchmarkTools.Trial:
memory estimate: 7.63 MiB
allocs estimate: 2
--------------
minimum time: 2.359 ms (0.00% GC)
median time: 2.713 ms (0.00% GC)
mean time: 3.794 ms (28.81% GC)
maximum time: 62.708 ms (95.27% GC)
--------------
samples: 1314
evals/sample: 1
julia> @benchmark C .= A .+ B
BenchmarkTools.Trial:
memory estimate: 128 bytes
allocs estimate: 4
--------------
minimum time: 1.232 ms (0.00% GC)
median time: 1.320 ms (0.00% GC)
mean time: 1.356 ms (0.00% GC)
maximum time: 2.572 ms (0.00% GC)
--------------
samples: 3651
evals/sample: 1
Note that memory usage dropped from 7.63MiB to 128 bytes.[1]: https://docs.julialang.org/en/stable/manual/functions/#man-v... |
|
> Note that memory usage dropped from 7.63MiB to 128 bytes.
Which is important if you're working with large data-sets. Both for performance and for being able to run the calculations at all.