note 115297 deleted from function.array-merge by salathe
| From: | salathe@php.net | Date: | Mon, 06 Sep 2021 22:46:00 +0000 |
| Subject: | note 115297 deleted from function.array-merge by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216901@lists.php.net to get a copy of this message | ||
Note Submitter: gj@php
----
i did a small benchmark (on PHP 5.3.3) comparing:
* using array_merge on numerically indexed arrays
* using a basic double loop to merge multiple arrays
the performance difference is huge:
<?php
require_once("./lib/Timer.php");
function method1($mat){
$all=array();
foreach($mat as $arr){
$all=array_merge($all,$arr);
}
return $all;
}
function method2($mat){
$all=array();
foreach($mat as $arr){
foreach($arr as $el){
$all[]=$el;
}
}
return $all;
}
function tryme(){
$y=250; //#arrays
$x=200; //size per array
$mat=array();
//build big matrix
for($i=0;$i<$y;$i++){
for($j=0;$j<$x;$j++){
$mat[$i][$j]=rand(0,1000);
}
}
$t=new Timer();
method1($mat);
$t->displayTimer();
$t=new Timer();
method2($mat);
$t->displayTimer();
/*
output:
Script runtime: 2.36782 secs. Script runtime: 0.02137 sec.
*/
}
tryme();
?>
So that's more than a factor 100!!!!!