forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quite faster rand(::MersenneTwister, ::Type{Int}) ... (JuliaLang#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...)
- Loading branch information
Showing
5 changed files
with
108 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.