Skip to content

Commit

Permalink
faster rand!(::MersenneTwister, ::Array{Bool})
Browse files Browse the repository at this point in the history
This uses the same optimizations as for other bits types,
and gives equivalent performance as for `UInt8` (at least
7x to 9x speedup in few tested cases).
  • Loading branch information
rfourquet committed Oct 30, 2019
1 parent 1d8ec2f commit 6674cac
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,10 @@ function rand!(r::MersenneTwister, A::UnsafeView{UInt128}, ::SamplerType{UInt128
end

for T in BitInteger_types
@eval rand!(r::MersenneTwister, A::Array{$T}, sp::SamplerType{$T}) =
(GC.@preserve A rand!(r, UnsafeView(pointer(A), length(A)), sp); A)
@eval function rand!(r::MersenneTwister, A::Array{$T}, sp::SamplerType{$T})
GC.@preserve A rand!(r, UnsafeView(pointer(A), length(A)), sp)
A
end

T == UInt128 && continue

Expand All @@ -598,6 +600,17 @@ for T in BitInteger_types
end
end


#### arrays of Bool

function rand!(r::MersenneTwister, A::Array{Bool}, sp::SamplerType{Bool})
GC.@preserve A rand!(r,
UnsafeView(Ptr{UInt8}(pointer(A)), length(A)),
SamplerType{UInt8}())
A
end


#### from a range

for T in BitInteger_types, R=(1, Inf) # eval because of ambiguity otherwise
Expand Down

0 comments on commit 6674cac

Please sign in to comment.