Allocations and in-place operations
Consider the following trivial function, xpow
, which takes an integer as input and returns the first few powers of the number. Another function, xpow_loop
, uses the first function to compute the sum of squares of a large sequence of numbers, as follows:
function xpow(x) return [x x^2 x^3 x^4] end function xpow_loop(n) s = 0 for i = 1:n s = s + xpow(i)[2] end return s end
Benchmarking this function for a large input shows that this function is quite slow, as follows:
julia> @benchmark xpow_loop(1000000) ================ Benchmark Results ======================== Time per evaluation: 103.17 ms [101.39 ms, 104.95 ms] Proportion of time in GC: 13.15% [12.76%, 13.53%] Memory allocated: 152.58 mb Number of allocations: ...
Get Julia: High Performance Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.