php方法传值和传引用性能比较

Posted 犀利瓜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php方法传值和传引用性能比较相关的知识,希望对你有一定的参考价值。

测试代码test.php

<?php

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

function a($arr) {
     $data = [];
      foreach($arr as $var) {
            $data[] = $var;
          }

       return $data;
}

function b(&$arr) {
     $data = [];
      foreach($arr as $var) {
            $data[] = $var;
          }

       return $data;
}

$arr = [
 [
  ‘aaa‘ => [
   ‘aaa‘=>‘bbb‘,
   ‘bbb‘=>‘bbb‘,
   ‘ccc‘=>‘bbb‘,
   ‘ddd‘=>‘bbb‘,
   ‘eee‘=>‘bbb‘,
   ‘fff‘=>‘bbb‘,
   ‘add‘=>‘bbb‘,
   ‘dse‘=>‘bbb‘,
   ‘dad‘=>‘bbb‘,
   ‘dsa‘=>‘bbb‘,
   ‘das‘=>‘bbb‘,
   ‘ewq‘=>‘bbb‘,
   ‘qer‘=>‘bbb‘,
   ‘qad‘=>‘bbb‘,
   ‘asz‘=>‘bbb‘,
   ‘gda‘=>‘bbb‘,
   ‘abc‘=>‘bbb‘,
   ‘vbv‘=>‘bbb‘,
   ‘ewq‘=>‘bbb‘,
   ‘tew‘=>‘bbb‘,
  ],
  ‘bbb‘ => [
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>‘bbb‘,
   ‘qaa‘=>[
                ‘aaa‘=>‘bbb‘,
                ‘bbb‘=>‘bbb‘,
                ‘ccc‘=>‘bbb‘,
                ‘ddd‘=>‘bbb‘,
                ‘eee‘=>‘bbb‘,
                ‘fff‘=>‘bbb‘,
                ‘add‘=>‘bbb‘,
                ‘dse‘=>‘bbb‘,
                ‘dad‘=>‘bbb‘,
                ‘dsa‘=>‘bbb‘,
                ‘das‘=>‘bbb‘,
                ‘ewq‘=>‘bbb‘,
                ‘qer‘=>‘bbb‘,
                ‘qad‘=>‘bbb‘,
                ‘asz‘=>‘bbb‘,
                ‘gda‘=>‘bbb‘,
                ‘abc‘=>‘bbb‘,
                ‘vbv‘=>‘bbb‘,
                ‘ewq‘=>‘bbb‘,
                ‘tew‘=>[
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>‘bbb‘,
                    ‘caa‘=>[
                        ‘aaa‘=>‘bbb‘,
                        ‘bbb‘=>‘bbb‘,
                        ‘ccc‘=>‘bbb‘,
                        ‘ddd‘=>‘bbb‘,
                        ‘eee‘=>‘bbb‘,
                        ‘fff‘=>‘bbb‘,
                        ‘add‘=>‘bbb‘,
                        ‘dse‘=>‘bbb‘,
                        ‘dad‘=>‘bbb‘,
                        ‘dsa‘=>‘bbb‘,
                        ‘das‘=>‘bbb‘,
                        ‘ewq‘=>‘bbb‘,
                        ‘qer‘=>‘bbb‘,
                        ‘qad‘=>‘bbb‘,
                        ‘asz‘=>‘bbb‘,
                        ‘gda‘=>‘bbb‘,
                        ‘abc‘=>‘bbb‘,
                        ‘vbv‘=>‘bbb‘,
                        ‘ewq‘=>‘bbb‘,
                        ‘tew‘=>[
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>‘bbb‘,
                            ‘qaa‘=>[
                                ‘aaa‘=>‘bbb‘,
                                ‘bbb‘=>‘bbb‘,
                                ‘ccc‘=>‘bbb‘,
                                ‘ddd‘=>‘bbb‘,
                                ‘eee‘=>‘bbb‘,
                                ‘fff‘=>‘bbb‘,
                                ‘add‘=>‘bbb‘,
                                ‘dse‘=>‘bbb‘,
                                ‘dad‘=>‘bbb‘,
                                ‘dsa‘=>‘bbb‘,
                                ‘das‘=>‘bbb‘,
                                ‘ewq‘=>‘bbb‘,
                                ‘qer‘=>‘bbb‘,
                                ‘qad‘=>‘bbb‘,
                                ‘asz‘=>‘bbb‘,
                                ‘gda‘=>‘bbb‘,
                                ‘abc‘=>‘bbb‘,
                                ‘vbv‘=>‘bbb‘,
                                ‘ewq‘=>‘bbb‘,
                                ‘tew‘=>‘bbb‘,
                            ],
                        ],
                    ],
                ],
            ],
  ],
        ‘ccc‘ => [
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>‘bbb‘,
            ‘caa‘=>[
                ‘aaa‘=>‘bbb‘,
                ‘bbb‘=>‘bbb‘,
                ‘ccc‘=>‘bbb‘,
                ‘ddd‘=>‘bbb‘,
                ‘eee‘=>‘bbb‘,
                ‘fff‘=>‘bbb‘,
                ‘add‘=>‘bbb‘,
                ‘dse‘=>‘bbb‘,
                ‘dad‘=>‘bbb‘,
                ‘dsa‘=>‘bbb‘,
                ‘das‘=>‘bbb‘,
                ‘ewq‘=>‘bbb‘,
                ‘qer‘=>‘bbb‘,
                ‘qad‘=>‘bbb‘,
                ‘asz‘=>‘bbb‘,
                ‘gda‘=>‘bbb‘,
                ‘abc‘=>‘bbb‘,
                ‘vbv‘=>‘bbb‘,
                ‘ewq‘=>‘bbb‘,
                ‘tew‘=>[
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>‘bbb‘,
                    ‘qaa‘=>[
                        ‘aaa‘=>‘bbb‘,
                        ‘bbb‘=>‘bbb‘,
                        ‘ccc‘=>‘bbb‘,
                        ‘ddd‘=>‘bbb‘,
                        ‘eee‘=>‘bbb‘,
                        ‘fff‘=>‘bbb‘,
                        ‘add‘=>‘bbb‘,
                        ‘dse‘=>‘bbb‘,
                        ‘dad‘=>‘bbb‘,
                        ‘dsa‘=>‘bbb‘,
                        ‘das‘=>‘bbb‘,
                        ‘ewq‘=>‘bbb‘,
                        ‘qer‘=>‘bbb‘,
                        ‘qad‘=>‘bbb‘,
                        ‘asz‘=>‘bbb‘,
                        ‘gda‘=>‘bbb‘,
                        ‘abc‘=>‘bbb‘,
                        ‘vbv‘=>‘bbb‘,
                        ‘ewq‘=>‘bbb‘,
                        ‘tew‘=>‘bbb‘,
                    ],
                ],
            ],
        ],
 ]
];
$time = microtime_float();
echo ‘first start: ‘.$time."
";

//for ($i = 0 ; $i<5000000; $i++) {
//     
//  a($arr);
//}
a($arr);

$end = microtime_float();
echo ‘first end:‘.$end."
";
echo ‘value used:‘.bcsub($end , $time,4);
echo "
";
echo "
";
echo "
";

$time = microtime_float();
echo ‘second start:‘.$time."
";

//for ($i = 0 ; $i<5000000; $i++) {
//  b($arr);
//}

b($arr);

$end = microtime_float();
echo ‘second end:‘.$end."
";
echo ‘second used:‘.bcsub($end,$time,4);
echo "
";

测试方法:

watch -n 1 -d php test.php

// 输出 
first start: 1539332451.0195
first end:1539332451.0197
value used:0.0002



second start:1539332451.0198
second end:1539332451.0198
second used:0.0000

first start: 1539332463.6168
first end:1539332463.6169
value used:0.0001



second start:1539332463.6169
second end:1539332463.6169
second used:0.0000

结论:

  • 传引用的性能要高于传值,特别是传递的值比较大的时候,性能会很明显。这是因为传引用只是复制了地址(一个16进制数)
  • 并发的瓶颈一般不会再php,而是在框架、在数据库。

以上是关于php方法传值和传引用性能比较的主要内容,如果未能解决你的问题,请参考以下文章

PHP传值和传引用传地址的区别

传值和传引用

在c语言编程中,传值方式和传引用方式之间有啥区别?

c语言 两个函数(传值和传引用)并且比较两者的不同,题目自设

传值和传址

php中传值与传引用的区别?