1. 程式人生 > >php方法傳值和傳引用性能比較

php方法傳值和傳引用性能比較

1.0 傳遞 function ++ func fir reac bbb 發的

測試代碼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."\n";

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

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

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

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

b($arr);

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

測試方法:

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方法傳值和傳引用性能比較