PHP變數引用與非引用效能比較

來源:互聯網
上載者:User
本文主要和大家分享PHP變數引用與非引用效能比較,希望能協助到大家。

<?php// PREVIEW:資料需要修改的就用引用,資料不需要修改的就用非引用$arrayCount = 2000;$cycleCount = 100000;// 向函數傳遞參數並做修改echo "向函數傳遞參數並做修改", PHP_EOL;function ByVal($p){return $p[intval(count($p) / 2)] += 1;}  function ByRef(&$p){return $p[intval(count($p) / 2)] += 1;}$p = array_fill(0, $arrayCount, 1);$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $a = ByVal($p);}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByValFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $a = ByRef($p);}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByRefFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL;// END: 速度相近,記憶體引用勝出// 向函數傳遞參數不做修改echo "向函數傳遞參數不做修改", PHP_EOL;function ByVal2($p){return $p[intval(count($p) / 2)];}  function ByRef2(&$p){return $p[intval(count($p) / 2)];}$p = array_fill(0, $arrayCount, 1);$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $a = ByVal2($p);}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByValFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $a = ByRef2($p);}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByRefFun: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL;// END: 傳值效能大幅勝出,記憶體相近// 變數賦值做修改echo "變數賦值做修改", PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    $a = $p;    $a[2] += 1;}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByVal: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    $a = &$p;    $a[2] += 1;}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL;// END 速度相近,引用略勝;記憶體引用大幅勝出// 變數賦值不做修改echo "變數賦值不做修改", PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    $a = $p;    $b = $a[2];}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByVal: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    $a = &$p;    $b = $a[2];}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL;// END 速度相近,記憶體相近// 數組內迴圈賦值修改資料echo "數組內迴圈賦值修改資料", PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    foreach($p as $key => $val){        $p[$key] += 1;    }}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByKey: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    foreach($p as &$val){        $val += 1;    }}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL , PHP_EOL;// END: 引用勝出// 數組內迴圈賦值不修改資料echo "數組內迴圈賦值不修改資料", PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    foreach($p as $key => $val){        $b = $val;    }}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByKey: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;$start = microtime(true);$startMem = Memory_get_usage();for($i = 0; $i < $cycleCount; $i += 1){    $p = array_fill(0, $arrayCount, 1);    foreach($p as &$val){        $b = $val;    }}$end = microtime(true);$endMem = Memory_get_usage();echo 'ByRef: ', $end - $start, 's' , PHP_EOL, 'Mem: ', $endMem - $startMem , PHP_EOL;// END: 傳值勝出// 總結:資料需要修改的就用引用,資料不需要修改的就用非引用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.