You can also use the let
block trick to solve the type stability issue, which is much easier than what I did above.
using QuantEcon
using BenchmarkTools, Compat # Time benckmarking
function valor(ind_α::Int64, α_ap::Array{Float64,2})
# Long iteration to compute something that depends on α
aux = zeros(100)
for ind_aux in 1:100
for ind_ap in 1:5000
aux[ind_aux] = aux[ind_aux] + α_ap[ind_α, ind_ap]
end
end
# Define interpolation
aux_inter = LinInterp(linspace(0.,254.,100), aux)
return_f = let aux_inter = aux_inter, ind_α = ind_α
function(ap::Float64)
# Compute consumption
c = ind_α + ap
return log(c) + 0.95*aux_inter(ap)
end
end
return return_f
end
function torna()
pf = zeros(5000)
V = zeros(5000)
α_ap = rand(5000,5000)
for ind_z in 1:500
# Get function to optimize
aux_f = valor(ind_z,α_ap)
# Solve
pf[ind_z], V[ind_z] = golden_method(aux_f, 0., 100.)
end
return pf, V
end
However, it’s pretty slow regardless.