note 115297 added to function.array-merge
| From: | gj at php | Date: | Tue, 01 Jul 2014 14:59:11 +0000 |
| Subject: | note 115297 added to function.array-merge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199797@lists.php.net to get a copy of this message | ||
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!!!!!
----
Server IP: 72.52.91.14
Probable Submitter: 213.127.249.194
----
Manual Page -- http://php.net/manual/en/function.array-merge.php
Edit -- https://master.php.net/note/edit/115297
Del: integrated -- https://master.php.net/note/delete/115297/integrated
Del: useless -- https://master.php.net/note/delete/115297/useless
Del: bad code -- https://master.php.net/note/delete/115297/bad+code
Del: spam -- https://master.php.net/note/delete/115297/spam
Del: non-english -- https://master.php.net/note/delete/115297/non-english
Del: in docs -- https://master.php.net/note/delete/115297/in+docs
Del: other reasons-- https://master.php.net/note/delete/115297
Reject -- https://master.php.net/note/reject/115297
Search -- https://master.php.net/manage/user-notes.php