简体   繁体   English

在函数内优化 PHP 中的大数组操作

[英]Optimizing big array manipulation in PHP inside a function

I want to modify a big array inside a function, so I'm pretty sure I need to use references there, but I'm not sure what of these two alternatives is better (more performant, but also maybe some side effects?):我想修改一个函数内的一个大数组,所以我很确定我需要在那里使用引用,但我不确定这两个替代方案中的哪个更好(性能更高,但也可能有一些副作用?):

$array1 = getSomeBigArray();
$array2 = getAnotherBigArray();
$results[] = combineArrays($array1, $array2);

function combineArrays(&$array1, $array2){
   // this is not important, just example of modification
   foreach($array2 as $value){
      if($value > 0){
         $array1[] = $value;
      }
   }
   return $array1;    // will returning $array1 make a copy?
}

Option 2:选项 2:

$array1 = getSomeBigArray();
$array2 = getAnotherBigArray();
combineArrays($array1, $array2);
$results[] = $array1;

function combineArrays(&$array1, $array2){

   foreach($array2 as $value){
      if($value > 0){
         $array1[] = $value;
      }
   }
   // void function
}

EDIT: I have run some tests and now I'm more confused.编辑:我已经运行了一些测试,现在我更困惑了。
This is the test:这是测试:
https://ideone.com/v7sepC https://ideone.com/v7sepC
From those results it seems to be faster to not use references at all!从这些结果来看,根本不使用引用似乎更快! and if used is faster option1 (with return).如果使用更快的选项1(带返回)。
But in my local env using references seems to be faster (not so much).但是在我的本地环境中,使用引用似乎更快(不是那么多)。

EDIT 2:编辑2:
Maybe there is a problem with ideone.com?也许ideone.com有问题? because running the same here:因为在这里运行相同:
https://3v4l.org/LaffP https://3v4l.org/LaffP
the result is: Opcion1 and Option2 (references) are almost equal and faster than passing by value结果是: Opcion1 和 Option2(引用)几乎相等并且比按值传递更快

When you do return $array1;当你return $array1; (in the first option) it does not copy the array, only increases the reference counter and returns the reference to the same array. (在第一个选项中)它复制数组,只增加引用计数器并返回对同一数组的引用。

Ie returning value of the function and $array1 will be pointing to the same array in the memory.即函数的返回值和$array1将指向内存中的同一个数组。 Unless you modify any of them: in that moment the data will be actually copied.除非您修改其中任何一个:在那一刻,数据将被实际复制。

The same happens when you are assigning a value to $results[] = $array1;当您为$results[] = $array1;赋值时,也会发生同样的情况$results[] = $array1; no data is actually copied, only a reference being put into a new element of $results .没有实际复制数据,只有一个引用被放入$results的新元素中。

In the end, both options have the same result: You'll have references to the same data in variable $array1 and in the last item of $results .最后,两个选项都有相同的结果:您将在变量$array1$results的最后一项中引用相同的数据。 Therefore, there is no notable performance difference in those two options.因此,这两个选项没有显着的性能差异。

Also, consider using native functions to perform typical actions.此外,请考虑使用本机函数来执行典型操作。 Eg array_merge()例如array_merge()

code 1: 1000000 values, resources: 32代码 1:1000000 个值,资源:32

code 1: 10000000 values, resources: 67代码 1:10000000 个值,资源:67

code 2: 1000000 values, resources: 27代码 2:1000000 个值,资源:27

code 2: 2000000 values, resources: 49代码 2:2000000 个值,资源:49

I calculated the resource usage of the system by calling我通过调用计算了系统的资源使用情况

getrusage

And code 2 seems to be more performant.代码 2 似乎性能更高。 You can use the following code to make some tests yourself:您可以使用以下代码自己进行一些测试:

<?php 

function getSomeBigArray() {
    $arr = [];
    for ($i=0;$i<2000000;$i++) {
        $arr[] = $i;
    }   
    return $arr;
}

function rutime($ru, $rus, $index) {
    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
 -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}

$array1 = getSomeBigArray();
$array2 = getSomeBigArray();

$rustart = getrusage();
$results[] = combineArrays($array1, $array2);
$ru = getrusage();

echo rutime($ru, $rustart, "utime");

function combineArrays(&$array1, $array2){
    // The array combining method.
}

Note : method rutime used was copied by the right answer of the following stackoverflow post: Tracking the script execution time in PHP注意:使用的方法 rutime由以下 stackoverflow 帖子的正确答案复制: Tracking the script execution time in PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM