# Monte Carlo particle simulations are very slow

**URL:** https://discourse.julialang.org/t/monte-carlo-particle-simulations-are-very-slow/39239
**Category:** Performance
**Created:** [May 10, 2020, 6:45pm UTC](https://discourse.julialang.org/t/monte-carlo-particle-simulations-are-very-slow/39239 "2020-05-10T18:45:23Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)
#### Post date: [May 10, 2020, 7:35pm UTC](https://discourse.julialang.org/t/monte-carlo-particle-simulations-are-very-slow/39239/3 "2020-05-10T19:35:15Z")

</div>

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](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-changing-the-type-of-a-variable-1) which describes this precise form of type instability.

---

_[View the full topic](https://discourse.julialang.org/t/monte-carlo-particle-simulations-are-very-slow/39239)._
