Hello,
While trying to optimise my code, Juno’s profiler flagged a portion of my code as doing some dynamic dispatch. Since my original code is fairly long, here is the smallest example I could come up with to illustrate what is going on:
using Profile
function gss(f::Function, a::Float64, b::Float64, tol::Float64 = 1e-5)::Float64
Gratio = Base.MathConstants.golden
c = b - (b - a) / Gratio
d = a + (b - a) / Gratio
while abs(c - d) > tol
if f(c) > f(d)
b = d
else
a = c
end
c = b - (b - a) / Gratio
d = a + (b - a) / Gratio
end
return (b + a) / 2.0
end
V(x::Float64, y::Float64) = x^2.0 + y^2.0
function solve_gss(a::Float64, b::Float64, V::Function)
# Define auxiliary function
aux_f(x::Float64) = x + V(x,a)
# Find maximum
aux_x = gss(aux_f, a, b)
return aux_f(aux_x), aux_x
end
function solve_z(a::Float64, c::Float64, d::Float64, V::Function, gp::Int64 = 100)
# Create grid for z
grid_z = range(c, stop = d, length = gp)
# Iterate over z
aux_Vs = Vector{Float64}(undef, gp)
aux_xs = Vector{Float64}(undef, gp)
for (ind_z, z_td) in enumerate(grid_z)
aux_Vs[ind_z], aux_xs[ind_z] = solve_gss(a, z_td, V)
end
# Find optimal z
(maxV, maxLoc) = findmax(aux_Vs)
return maxV, grid_z[maxLoc], aux_xs[maxLoc]
end
@profiler for i = 1:10000
solve_z(0.0, 0.0, 10.0, V)
end
According to my limited understanding of Juno’s profiler, the issue is on the following line within function solve_z
:
aux_Vs[ind_z], aux_zs[ind_z] = solve_gss(a, z_td, V)
I’m confused because as far as I can see solve_gss
is simply returning a Tuple{Float64,Float64}
which is then stored in two arrays. I’ve tried specifying the return of solve_gss
as a Tuple{Float64,Float64}
:
solve_gss(a::Float64, b::Float64, V::Function)::Tuple{Float64,Float64}
But that does not help either.
Any help is greatly appreciated.