My tests revealed that the lower number of random numbers you calculate in a script, the less efficient mt_rand() is.Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the rand() function. The mt_rand() function is a drop-in replacement for this. It uses a random number generator with known characteristics using the Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.
Code: Select all
$start = getmicrotime();
for($i=0;$i<5;$i++){
$var .= mt_rand(1,100);
}
$end = getmicrotime();
for($i=0;$i<5;$i++){
$var .= rand(1,100);
}
$end2 = getmicrotime();
$test1 = $end - $start;
$test2 = $end2 - $end;Make it 1million? mt_rand wins again
Make it under 10,000? rand() is more efficient.
Can anyone else test this to see if I'm doing it wrong, or something?
I'm just wondering why it'd be in the manual if it's not true..