首頁 > 後端開發 > php教程 > PHP變數引用與非引用效能比較

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

小云云
發布: 2023-03-21 21:20:02
原創
1982 人瀏覽過

本文主要和大家分享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 &#39;ByValFun: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByRefFun: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByValFun: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByRefFun: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByVal: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByRef: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByVal: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByRef: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByKey: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByRef: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByKey: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $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 &#39;ByRef: &#39;, $end - $start, &#39;s&#39; , PHP_EOL, &#39;Mem: &#39;, $endMem - $startMem , PHP_EOL;
// END: 传值胜出

// 总结:数据需要修改的就用引用,数据不需要修改的就用非引用
登入後複製

相關推薦:

php變數參考、函數參考、物件引用三種引用實例程式碼詳解

PHP變數參考、函數參考、物件參考

PHP變數引用 PHP 全域變數

以上是PHP變數引用與非引用效能比較的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板