Last active
February 25, 2020 23:15
-
-
Save MasonProtter/73e427d8005dd1eb07a7211876c0114f to your computer and use it in GitHub Desktop.
Pi monte carlo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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