I’m working on optimizing some code. I isolated small functions, I benchmarked them and I was happy with their speed. But when I bundle those functions in an outer function, the timing almost twice the sum of the isolated function… Any hints on what’s wrong?
Here a simplified version of my code:
function toPolar!(x)
for i = 1:size(x, 1)
x[i, 1], x[i, 2] = sqrt(x[i, 1]^2 + x[i, 2]^2), atan(x[i, 2], x[i, 1])
end
end
function toCartesian!(x)
for i = 1:size(x, 1)
x[i, 1], x[i, 2] = x[i, 1] * cos(x[i, 2]), x[i, 1] * sin(x[i, 2])
end
end
function move!(x, v, T)
for i = 1:size(x, 1)
x[i, 1] += v[i, 1] * T
x[i, 2] += v[i, 2] * T
end
end
function outerFunction!(x, v, T)
toCartesian!(x)
move!(x, v, T)
toPolar!(x)
end
And here the benchmarks:
julia> using BenchmarkTools
julia> x = rand(10000, 2)
julia> v = rand(10000, 2)
julia> T = 1.0
julia> @btime toPolar!($x)
232.784 μs (0 allocations: 0 bytes)
julia> @btime toCartesian!($x)
101.394 μs (0 allocations: 0 bytes)
julia> @btime move!($x, $v, $T)
14.378 μs (0 allocations: 0 bytes)
julia> @btime outerFunction!($x, $v, $T)
612.012 μs (0 allocations: 0 bytes)
I would have expected the total timing to be approximately 233 + 101 + 14 = 350 μs
Any help is welcome!