julia> function findpi(n)
inside = 0.
for i in 1:n
inside += rand()^2 + rand()^2 <= 1 ? 1. : 0.
end
4 * inside / n
end
findpi (generic function with 1 method)
julia> @benchmark findpi(10000000)
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 43.458 ms (0.00% GC)
median time: 44.101 ms (0.00% GC)
mean time: 44.429 ms (0.00% GC)
maximum time: 54.051 ms (0.00% GC)
--------------
samples: 113
evals/sample: 1
Also note that the mixing of float and Int was not a problem in your original code. Everything was type stable, as inside started out as an int and stayed an Int.