Fastest pseudo-random number-generator: erlang:statistics(io) ?

Thijs thijsterlouw@REDACTED
Tue Nov 24 16:00:09 CET 2009


Today I needed a faster alternative for erlang:now(). It turns out
that using erlang:now() is quite slow: it requires a system call. I
decided to test some alternatives. My requirements are: speed, speed,
speed :) I will use it in a server that is quite busy, so I figured I
could use various system metrics to generate random numbers. I use
these pseudo-random numbers to do load-balancing, log requests at
random etc.

benchmark results :

erlang:now()  	288.090
erlang:statistics(io) 	2.730.853
erlang:statistics(wall_clock) 	280.396
erlang:statistics(runtime) 	510.765
erlang:statistics(reductions) 	471.688
erlang:statistics(context_switches) 	493.851
random:uniform()   	870.261

This test is spawning 100 concurrent processes, each executing 10000
calls to each function. Two notes:
1. Note that you can only use this function if your server is actually
performing (lots) of IO
2. random:uniform() needs to be seeded for real use (each process
starts the same), I didn't do that.

Based on these tests, erlang:statistics(io) is the clear winner. I
will use it now for picking random elements from lists and replacing
pg2:get_closest_pid(Name).


Does anyone have any faster suggestions than erlang:statistics(io) ?


More information about the erlang-questions mailing list