Skip to content

Commit 3d71a2b

Browse files
adienesdevmotion
andauthored
microoptimization on rand(::AliasTable) (#1831)
* microoptimization on rand(::AliasTable) * Update src/samplers/aliastable.jl Co-authored-by: David Widmann <[email protected]> --------- Co-authored-by: David Widmann <[email protected]>
1 parent c1705a3 commit 3d71a2b

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/samplers/aliastable.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ end
1515

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

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

0 commit comments

Comments
 (0)