julia
b8bc8167 - quite faster rand(::MersenneTwister, ::Type{Int}) ... (#37914)

Commit
5 years ago
quite faster rand(::MersenneTwister, ::Type{Int}) ... (#37914) ... when the quest for superficial beauty leads to performance ... The recent implementation of `show` for `MersenneTwister` was not ideal, as a number of book-keeping variables had to be introduced as fields of MT; in particular, as generating the cache for ints (using the generic `rand!` for integer arrays) was consuming random `Float64` numbers, 4 integers had to be shown in `show` only to reproduce the state of the ints cache. In total 8 integers were shown. But it is not so difficult to improve a bit, thanks to two features of the internal cache: 1) it's 16-byte aligned, so the dSFMT low-level routine can be called directly on it (whereas the generic `rand!` for integers has to take care that the same stream is produced whatever the alignment) 2) it can be resized: dSFMT randomizing only 52 out of 64 bits, i.e. a bit more than 80% of the bits, the trick is to count the total number of needed bits, grow the array to a size such that dSFMT produces these needed bits, and then condense these bits back into a 100% randomized array of the original size As a net result, two variables could be deleted which `show` doesn't needs to display anymore. Purely as a side effect, scalar generation of `Int64`/`UInt64` has a speedup of about 1.8x, and about 1.6x for `Int128`/`UInt128`, and about 1.3x or 1.5x for generation in a range of integers of size with less than 64 bits, e.g. `rand(1:9)` (at least on this machine...)
Author
Parents
Loading