php 自訂函數和原生函數執行效率問題

來源:互聯網
上載者:User

標籤:io   ar   使用   for   on   問題   cti   演算法   php   

從網上看到的,有些時候原生函數的效率可能沒有自訂的函數高

例如range函數,用它來初始化一個索引值與鍵名相等的,且鍵名差值相同的數組,原生函數使用效率反而不高,

這可能是因為range函數不僅僅實現了這一種需求,這個函數還可以接受一些其他參數去執行演算法,傳回值

所以當我們僅需要某個功能時,可能使用自己的函數會運行更快,原生函數會經過各種判斷,才能執行想要的方法(自己想的,可能不對)

例:

function ranges($step = 5, $max = 20)
{
if ($step < 1)
return array();

$array = array();
for ($i = $step; $i <= $max; $i += $step)
{
$array[$i] = $i;
}

return $array;
}


$time[‘begin‘] = microtime(true);
//$tmp = range(0,1000000,3);
$tmp = ranges(0,1000000,3);
$time[‘end‘] = microtime(true);


echo $time[‘end‘] - $time[‘begin‘].‘s‘."\r";
echo (memory_get_peak_usage())."M";

自訂:

7.8678131103516E-6s 0.59011840820312M

原生

0.069043874740601s 68.172889709473M

php 自訂函數和原生函數執行效率問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.