# Advice for improving Monte-Carlo code

**URL:** <https://discourse.julialang.org/t/advice-for-improving-monte-carlo-code/49782>\
**Category:** New to Julia\
**Tags:** performance, parallel, monte-carlo\
**Created:** [November 8, 2020, 10:41am UTC](https://discourse.julialang.org/t/advice-for-improving-monte-carlo-code/49782 "2020-11-08T10:41:14Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 8, 2020, 12:33pm UTC](https://discourse.julialang.org/t/advice-for-improving-monte-carlo-code/49782/8 "2020-11-08T12:33:25Z")

</div>

Somewhat related, for MC calculations have a look at  
[https://baggepinnen.github.io/MonteCarloMeasurements.jl/latest/](https://baggepinnen.github.io/MonteCarloMeasurements.jl/latest/)  
Awesome package which makes it easy to do MC. Your original example becomes:

```julia
julia> using MonteCarloMeasurements, Distributions

julia> n = 1000;

julia> p1 = [Particles(n, Uniform(0,1)), Particles(n, Uniform(0,1))]
2-element Array{Particles{Float64,1000},1}:
 0.5 ± 0.29
 0.5 ± 0.29

julia> p2 = [Particles(n, Uniform(0,1)), Particles(n, Uniform(0,1))];

julia> dist(p1, p2) = sqrt((p1[1]-p2[1])^2 + (p1[2]-p2[2])^2)
dist (generic function with 1 method)

julia> dist(p1,p2)
Particles{Float64,1000}
 0.523052 ± 0.25

```

I think it’s fast too, here the timing on a new laptop:

```julia
julia> @btime dist(p1,p2)
  9.568 μs (7 allocations: 47.64 KiB)
Particles{Float64,1000}
 0.523052 ± 0.25

```

---

_[View the full topic](https://discourse.julialang.org/t/advice-for-improving-monte-carlo-code/49782)._
