I am working on a dynamic programming assignment for macro-economics that has code in Matlab which I tried to translate to Julia.

However I cannot get the performance to be what it should be.

Here is the loop solving the Bellman equation in each iteration

```
function perform_loop(
size_state_space::Int64,
utilities::Matrix{Float64},
optimal_choices::Vector{Int64},
current_utilities::Vector{Float64},
max_utilities::Vector{Float64},
absdiff::Vector{Float64})
for vfit = 1:VFMAXIT
#form Bellman equation
@inbounds for i in 1:size_state_space
u, ind = findmax(view(utilities, :, i) .+ β .* current_utilities)
optimal_choices[i] = ind
max_utilities[i] = u
end
# Vector of errors
absdiff .= abs.(max_utilities .- current_utilities)
# Maximum absolute error
vferr = maximum(absdiff)
#if (mod(vfit, 50) == 1)
# println("VF error = $vferr on VF iteration $vfit.")
#end
# exit if converged
if (vferr < VFTOL)
println("Number of iterations: $vfit")
println("Error achieved: $vferr")
break
end
# if not converged, then redo
current_utilities .= max_utilities
end
return optimal_choices
end
```

Here `size_state_space`

is `1000`

, which means that the length for everything is `1000`

. Utilities is a `1000`

by `1000`

matrix indicating the `utility`

for every combination of elements in the state space, which is equal to `range(.001, stop = 1, length = 1000)`

. It has many values equal to `-Inf`

because some combinations are not feasible.

I feel like I have done a good job pre-allocating arrays and only using broadcasted assignment. I have also made sure to iterate column-wise over `utilities`

for fastest iteration. As you can see, the bulk of my computation, done in the loop, has been placed in it’s own function. Nonetheless my code still takes twice as long as similar code in matlab.

I have made use of Profile and ProfileViews to better understand what is taking up so much time, but I have yet to have a breakthrough.

I am sure the problem will be obvious as I have not had much experience writing code that needs to be fast. Any help is appreciated.