Benchmark Random Number Generators in MATLAB with CompareRNG
Let's see how long it takes to generate 20 million random numbers on my laptop
tic;vec=rand(1,20000000);toc
That's pretty fast but I can do even better by switching to a different algorithm
rng("simdTwister")
tic;vec=rand(1,20000000);toc
MATLAB contains several high quality random number generation algorithms and the one it uses is controlled by the rng function as I show here. It turns out that I can do even better than the above!
rng("threefry")
tic;vec=rand(1,20000000);toc
Showing these three algorithms is the beginning of a deep rabbit hole that I go down during a course that I give on High Performance Computing in MATLAB. It turns out, that the fastest algorithm is machine dependent and I discovered this while asking people to try something like the above and to shout out which was the best algorithm on their machine.
Which is the fastest random number generation algorithm on your machine?
This is where today's pick of the week comes in. Developed by MathWorker, Ben Tordoff, CompareRNG runs all of the high quality random number generation algorithms in MATLAB and shows you which is best on your machine. Here's the result for my Apple M2 Mac

and here is the result for my aging Intel desktop

in both cases, the 'Threefry' algorithm is the fastest but this isn't always the case. When I've been on the road with this benchmark, some people find that 'simdTwister' or 'philox' is better, for example.
The app also benchmarks GPU random number generators and the difference between those and the CPU-based generators is enormous!

Parallelization and random number generation algorithms
So what is it that makes the fastest random number generation algorithm machine dependent? Part of the answer is 'parallelization'. Some algorithms (e.g. 'twister' and 'simdTwister') only use one CPU core, no matter how many you actually have. Others, such as 'threefry' run in parallel and the results can be very impressive at high core counts.
This is where Ben's second new entry comes in useful. RNGMultiThreading show's how the performance of each algorithm varies as we increase the number of CPU cores. Here are the results on my Mac

We now get some insight into why we have machine dependent results.'philox' and 'threefry' does great when you use a lot of CPU cores but is not so great at lower core counts.
Give these new apps a try on your own machines and see which of these algorithms are best for you.
댓글
댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.