# Written in Julia 1.7.0
using Random, Distributions, BenchmarkTools
# See docs: http://juliastats.org/Distributions.jl/stable/starting/
# set seed
Random.seed!(123);
# base Julia has rand() and randn()
rand() # random [0, 1]
randn() # random Normal(μ=0, σ=1)
-1.4632513788889214
how long does it take to sample 1M from a normal distribution?
# sample from normal distribution
@benchmark s = rand(Normal(5, 2), 10^6)
BenchmarkTools.Trial: 946 samples with 1 evaluation. Range (min … max): 4.317 ms … 10.960 ms ┊ GC (min … max): 0.00% … 25.03% Time (median): 4.923 ms ┊ GC (median): 0.00% Time (mean ± σ): 5.282 ms ± 803.382 μs ┊ GC (mean ± σ): 5.40% ± 9.83% ▃█▂█▁ ▂▃▄▅▅██████▆▅▇▄▃▄▄▃▃▄▃▃▄▃▄▃▄▄▃▄▄▄▄▄▄▂▃▃▃▃▂▃▃▂▂▃▂▃▂▂▂▂▂▂▂▂▁▂ ▃ 4.32 ms Histogram: frequency by time 7.78 ms < Memory estimate: 7.63 MiB, allocs estimate: 2.
This is....really fast.
how long does it take to sample 1M from a triangular distribution?
note: Triangular takes arguments (min, max, mode) whereas numpy took (min, mode, max)
@benchmark rand(TriangularDist(0, 5, 3), 10^6)
BenchmarkTools.Trial: 517 samples with 1 evaluation. Range (min … max): 8.436 ms … 17.522 ms ┊ GC (min … max): 0.00% … 20.14% Time (median): 9.096 ms ┊ GC (median): 0.00% Time (mean ± σ): 9.669 ms ± 1.212 ms ┊ GC (mean ± σ): 3.82% ± 7.18% ▃▆█▂▁ ▃▄▇█████▇▄▄▄▃▃▄▃▄▃▄▂▃▃▄▄▃▄▄▃▄▃▃▃▂▃▂▃▃▃▂▂▂▂▂▂▂▂▂▂▁▃▂▁▁▁▁▁▁▂ ▃ 8.44 ms Histogram: frequency by time 14.1 ms < Memory estimate: 7.63 MiB, allocs estimate: 2.
So ~10ms vs python's 435 ms. Wow!
Let's try 1M samples from a discrete distribution with 3 potential values:
d = Distributions.DiscreteNonParametric([0, 1, 2], [0.2, 0.7, 0.1])
DiscreteNonParametric{Int64, Float64, Vector{Int64}, Vector{Float64}}(support=[0, 1, 2], p=[0.2, 0.7, 0.1])
@benchmark rand(d, 10^6)
BenchmarkTools.Trial: 397 samples with 1 evaluation. Range (min … max): 10.955 ms … 18.581 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 12.150 ms ┊ GC (median): 0.00% Time (mean ± σ): 12.602 ms ± 1.195 ms ┊ GC (mean ± σ): 2.94% ± 5.79% ▁ ▄█▆▄ ▂ ▂▂▁▄▅████████▇▆█▅▅▆▅▇▅▅▄▄▄▃▄▄▅▆▄▅▂▃▄▃▄▂▂▃▂▂▂▃▃▂▃▃▃▃▂▁▂▁▂▁▃▂ ▃ 11 ms Histogram: frequency by time 16.2 ms < Memory estimate: 7.63 MiB, allocs estimate: 7.
Discrete distribution takes about 13 ms vs the 43 ms it took Python, still very impressive.