Skip to content

Commit

Permalink
fix slowness of randn introduced by 84b6aec
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Nov 8, 2014
1 parent 64aa84d commit 12420d2
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ const ziggurat_exp_r = 7.6971174701310497140446280481

@inline randi(rng::MersenneTwister=GLOBAL_RNG) = reinterpret(Uint64, rand_close1_open2(rng)) & 0x000fffffffffffff

# the @inline'd rand(rng) method makes randmtzig_randn slower
rand_noinline(rng::MersenneTwister) = rand(rng)

function randmtzig_randn(rng::MersenneTwister=GLOBAL_RNG)
@inbounds begin
while true
Expand All @@ -824,13 +827,13 @@ function randmtzig_randn(rng::MersenneTwister=GLOBAL_RNG)
return x # 99.3% of the time we return here 1st try
elseif idx == 0
while true
xx = -ziggurat_nor_inv_r*log(rand(rng))
yy = -log(rand(rng))
xx = -ziggurat_nor_inv_r*log(rand_noinline(rng))
yy = -log(rand_noinline(rng))
if yy+yy > xx*xx
return (rabs & 0x100) != 0x000000000 ? -ziggurat_nor_r-xx : ziggurat_nor_r+xx
end
end
elseif (fi[idx] - fi[idx+1])*rand(rng) + fi[idx+1] < exp(-0.5*x*x)
elseif (fi[idx] - fi[idx+1])*rand_noinline(rng) + fi[idx+1] < exp(-0.5*x*x)
return x # return from the triangular area
end
end
Expand Down

0 comments on commit 12420d2

Please sign in to comment.