Skip to content

Instantly share code, notes, and snippets.

@MasonProtter
Last active February 25, 2020 23:15
Show Gist options
  • Save MasonProtter/73e427d8005dd1eb07a7211876c0114f to your computer and use it in GitHub Desktop.
Save MasonProtter/73e427d8005dd1eb07a7211876c0114f to your computer and use it in GitHub Desktop.
Pi monte carlo
using BenchmarkTools
function _pi(x::Vector, y::Vector)
acc = 0
@inbounds @simd for i ∈ eachindex(x)
acc += (x[i]*x[i] + y[i]*y[i]) < 1.0
end
4acc/length(x)
end
function pi_mc(nsamples)
_pi(rand(nsamples), rand(nsamples))
end
@btime pi_mc(10000000) # 54.796 ms (4 allocations: 152.59 MiB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment