Skip to content

Instantly share code, notes, and snippets.

@MasonProtter
Last active February 26, 2020 00:52
Show Gist options
  • Save MasonProtter/37647e3af2aba22c50b922d00fb1471a to your computer and use it in GitHub Desktop.
Save MasonProtter/37647e3af2aba22c50b922d00fb1471a to your computer and use it in GitHub Desktop.
using BenchmarkTools, StaticArrays, Transducers
function _pi_kernel_static()
xs = @SVector rand(50)
ys = @SVector rand(50)
sum(((x, y),) -> x^2 + y^2 < 1.0, zip(xs, ys))
end
function _pi_kernel(r)
xs = rand(r)
ys = rand(r)
sum(((x, y),) -> x^2 + y^2 < 1.0, zip(xs, ys))
end
function pi_mc(nsamples)
acc = 0
n, remainder = divrem(nsamples, 50)
acc += reduce(+, Map(_ -> _pi_kernel_static()), 1:n)
if remainder != 0
acc += _pi_kernel(remainder)
end
return 4acc/nsamples
end
@benchmark pi_mc(x) setup=(x=10_000_000)
#= On a Ryzen 5 2600 using 6 threads, I get
BenchmarkTools.Trial:
memory estimate: 16.56 KiB
allocs estimate: 313
--------------
minimum time: 10.754 ms (0.00% GC)
median time: 10.993 ms (0.00% GC)
mean time: 11.039 ms (0.00% GC)
maximum time: 14.221 ms (0.00% GC)
--------------
samples: 453
evals/sample: 1
=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment