In addition to the problems that @Oscar_Smith mentioned, in your energy
function you initialise total_energy
as an Int
, and inside the loop it becomes a Float64
. In the end the compiler can’t know exactly which type total_energy
has, which leads to poor performance.
In this specific case you could initialise total_energy = 0.0
to fix the issue.
You may want to take a look at the performance tips in the Julia docs, and in particular to this section which describes this precise form of type instability.