I am working on a code base to solve a large dynamic economic model with heterogeneous agents.
I coded up two different iterative algorithms for solving the model. These are wrapped in functions that I’ll call f1 and f2. These each make repeated calls to functions that carry out a single iteration step, call these g1 and g2.
So f1 looks somethign like:
function f1!(x,tol)
while resid<tol
xnew = g1(x)
resid = abs(xnew - xold)
@show resid
x = xnew
end
end
f2 would have the same structure, but calling g2 instead of g1.
g1 and g2 inturn make calls to some shared helper functions (also to functions that are unique to each) but do not calls each other.
I’m finding that the execution speed of f2 (and by extension g2) is dramatically faster if I call if g1 just once before executing f2. Since f2 performs many iteration, it seems unlikely that this would be a compile time issue; timing using @time shows that compile time is minimal either when g1 has been called first or otherwise.
I tried profiling the code and got quite strange results. Here is the profile for g2 when g1 has been called first (using ProfileView.jl):
And here is the profile for g2 when g1 was not called at all (i.e., start julia, then call only g2):
The red areas in both profiles are “boot.jl, eval”, which from what I understand is to be expected given I was calling the functions from the REPL.
I don’t know what to make of the large white space to the left of the second flame chart. It almost looks like the first chart was simply squeezed to the right but a close inspection shows this is not exactly the case.
Grateful for any ideas anyone might have.

