November 2016
Intermediate to advanced
697 pages
14h 44m
English
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
endBenchmarking 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: ...Read now
Unlock full access