Skip to content

Commit

Permalink
microoptimization on rand(::AliasTable) (#1831)
Browse files Browse the repository at this point in the history
* microoptimization on rand(::AliasTable)

* Update src/samplers/aliastable.jl

Co-authored-by: David Widmann <devmotion@users.noreply.github.com>

---------

Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
  • Loading branch information
adienes and devmotion authored Feb 5, 2024
1 parent c1705a3 commit 3d71a2b
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/samplers/aliastable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ end

function rand(rng::AbstractRNG, s::AliasTable)
i = rand(rng, 1:length(s.alias)) % Int
u = rand(rng)
@inbounds r = u < s.accept[i] ? i : s.alias[i]
r
# using `ifelse` improves performance here: github.com/JuliaStats/Distributions.jl/pull/1831/
ifelse(rand(rng) < s.accept[i], i, s.alias[i])
end

show(io::IO, s::AliasTable) = @printf(io, "AliasTable with %d entries", ncategories(s))

0 comments on commit 3d71a2b

Please sign in to comment.