php中隨機(jī)函數(shù)mt_rand()與rand()性能對比分析
本文實(shí)例對比分析了php中隨機(jī)函數(shù)mt_rand()與rand()性能問題。分享給大家供大家參考。具體分析如下:
在php中mt_rand()和rand()函數(shù)都是可以隨機(jī)生成一個純數(shù)字的,他們都是需要我們設(shè)置好種子數(shù)據(jù)然后生成,那么mt_rand()和rand()那個性能會好一些呢,下面我們帶著疑問來測試一下.
例子1. mt_rand() 范例,代碼如下:
echo mt_rand() . "n";
echo mt_rand() . "n";
echo mt_rand(5, 15);
?>
上例的輸出類似于:
1604716014
1478613278
6
注:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函數(shù)給隨機(jī)數(shù)發(fā)生器播種,現(xiàn)已自動完成.
注:在 3.0.7 之前的版本中,max 的含義是 range,要在這些版本中得到和上例相同 5 到 15 的隨機(jī)數(shù),簡短的例子是 mt_rand (5, 11).
詳情可查閱 mt_srand(),mt_getrandmax() 和 rand()相關(guān)文檔.
rand() 函數(shù)返回隨機(jī)整數(shù).
語法:rand(min,max)
參數(shù) | 描述 |
min,max | 可選,規(guī)定隨機(jī)數(shù)產(chǎn)生的范圍. |
說明:如果沒有提供可選參數(shù) min 和 max,rand() 返回 0 到 RAND_MAX 之間的偽隨機(jī)整數(shù),例如,想要 5 到 15(包括 5 和 15)之間的隨機(jī)數(shù),用 rand(5, 15).
提示和注釋
注釋:在某些平臺下(例如 Windows)RAND_MAX 只有 32768,如果需要的范圍大于 32768,那么指定 min 和 max 參數(shù)就可以生成大于 RAND_MAX 的數(shù)了,或者考慮用 mt_rand() 來替代它.
注釋:自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 函數(shù)給隨機(jī)數(shù)發(fā)生器播種,現(xiàn)在已自動完成.
注釋:在 3.0.7 之前的版本中,max 的含義是 range,要在這些版本中得到和上例相同 5 到 15 的隨機(jī)數(shù),簡短的例子是 rand (5, 11).
mt_rand()真的會比rand()快4倍嗎?帶著這個疑問一邊自己測試一邊看網(wǎng)上的介紹.測試如下.
mt_rand()和rand()對比測試一,測試代碼如下:
$max = 100000;
$timeparts = explode(' ',microtime());
$stime = $timeparts[1].substr($timeparts[0],1);
$i = 0;
while($i < $max) {
rand();
$i++;
}
$timeparts = explode(' ',microtime());
$etime = $timeparts[1].substr($timeparts[0],1);
$time = $etime-$stime;
echo "{$max} random numbers generated in {$time} seconds using rand();";
$timeparts = explode(' ',microtime());
$stime = $timeparts[1].substr($timeparts[0],1);
$i = 0;
while($i < $max) {
mt_rand();
$i++;
}
$timeparts = explode(' ',microtime());
$etime = $timeparts[1].substr($timeparts[0],1);
$time = $etime-$stime;
echo "{$max} random numbers generated in {$time} seconds using mt_rand(); ";
?>
測試結(jié)果如下:
//第一次測試
100000 random numbers generated in 0.024894952774048 seconds using rand();
100000 random numbers generated in 0.028925895690918 seconds using mt_rand();
//第二次測試
100000 random numbers generated in 0.03147292137146 seconds using rand();
100000 random numbers generated in 0.02997088432312 seconds using mt_rand();
//第三次測試
100000 random numbers generated in 0.028102874755859 seconds using rand();
100000 random numbers generated in 0.02803111076355 seconds using mt_rand();
//第四次測試
100000 random numbers generated in 0.025573015213013 seconds using rand();
100000 random numbers generated in 0.028030157089233 seconds using mt_rand();
這個結(jié)果只是幾次的顯示結(jié)果,多測試幾次你會發(fā)覺,兩者是交替變化的,其實(shí)兩者沒有太大的差異.
mt_rand()和rand()對比測試二
本人測試環(huán)境,操作系統(tǒng):windows xp,apache 2.0,php 5.2.12,內(nèi)存 2G
代碼如下:
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
for($i=0; $i<1000000; ++$i)
{
rand();
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "rand() cost $time secondsn";
$time_start = microtime_float();
for($i=0; $i<1000000; ++$i)
{
mt_rand();
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "mt_rand() cost $time secondsn";
?>
測試結(jié)果如下:
//第一次
rand() cost 0.25919604301453 seconds
mt_rand() cost 0.28554391860962 seconds
//第二次
rand() cost 0.31136202812195 seconds
mt_rand() cost 0.28973197937012 seconds
//第三次
rand() cost 0.27545690536499 seconds
mt_rand() cost 0.27108001708984 seconds
//第四次
rand() cost 0.26263308525085 seconds
mt_rand() cost 0.27727103233337 seconds
結(jié)果還是一樣:兩者用的時間是交替變化,其實(shí)兩者沒有太大的差異.
php的mt_rand()與rand()對比結(jié)論
在網(wǎng)上看了很多別人的測試,有l(wèi)inux的還有windows環(huán)境的,大多數(shù)人得出的結(jié)果和我的一樣:兩者相差無幾,不過也有人測出mt_rand()比rand()快4倍,但是由于他們沒給出具體的測試環(huán)境,所以無法判斷真假。我還是比較相信我的結(jié)論,因為我看到有人這樣介紹mt_rand()與rand():
那為什么php手冊上說mt_rand()比rand()快4倍呢?
這是因為mt_rand()使用的Mersenne Twister algorythm是1997的事,所以在10年前,和rand()在速度上的差異是(4倍),自2004年,rand()已經(jīng)開始使用algorythm,所以現(xiàn)在它們速度上沒有太大的區(qū)別.
從上面的各種測試來看它們之間并沒有區(qū)別,只是在不同系統(tǒng)中可能數(shù)值會有變化了.
希望本文所述對大家的PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)登錄搜狐廣告獲取廣告聯(lián)盟數(shù)據(jù)的方法【附demo源碼】
這篇文章主要介紹了PHP實(shí)現(xiàn)登錄搜狐廣告獲取廣告聯(lián)盟數(shù)據(jù)的方法,涉及php基于curl的遠(yuǎn)程數(shù)據(jù)操作相關(guān)技巧,需要的朋友可以參考下2016-10-10PHP對象的淺復(fù)制與深復(fù)制的實(shí)例詳解
這篇文章主要介紹了PHP對象的淺復(fù)制與深復(fù)制的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10