Benchmarks

Comparison with AdaptiveRejectionSampling.jl

using ARS, AdaptiveRejectionSampling
using DifferentiationInterface, Distributions
using ForwardDiff
using Chairmarks

# Define function to sample from
f(x) = logpdf(Laplace(0., 0.5), x) + logpdf(Normal(0.0, 2.0), x)
f (generic function with 1 method)

ARS.jl

#= ARS.jl =#
sam_ARS = ARS.ARSampler(ARS.Objective(f, AutoForwardDiff()), [-0.5, 0.5], (-Inf, Inf))

@be deepcopy(sam_ARS) ARS.sample!(_, 100000, true, 25) samples=100 evals=1
Benchmark: 91 samples with 1 evaluation
 min    10.455 ms (17 allocs: 784.492 KiB)
 median 10.707 ms (20 allocs: 784.586 KiB)
 mean   11.034 ms (21.15 allocs: 785.177 KiB, 0.98% gc time)
 max    24.432 ms (29 allocs: 789.367 KiB, 55.34% gc time)

AdaptiveRejectionSampling.jl

#= AdaptiveRejectionSampling.jl =#

sam_other = RejectionSampler(f, (-Inf, Inf), (-0.5, 0.5); logdensity=true, max_segments=25)

@be deepcopy(sam_other) run_sampler!(_, 100000) samples=100 evals=1 seconds=1000
Benchmark: 100 samples with 1 evaluation
 min    52.594 ms (2201413 allocs: 37.415 MiB)
 median 54.457 ms (2201754.50 allocs: 37.422 MiB, 3.57% gc time)
 mean   59.680 ms (2201773.71 allocs: 37.422 MiB, 6.15% gc time)
 max    185.641 ms (2202272 allocs: 37.430 MiB, 70.62% gc time)