note 55648 added to language.references.pass
| From: | jw at shapers dot nl | Date: | Tue, 09 Aug 2005 17:08:48 +0000 |
| Subject: | note 55648 added to language.references.pass | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93324@lists.php.net to get a copy of this message | ||
Don't use pass-by-reference when you want to modify large arrays. Instead, just pass by copy
and return the modified array, or use the PHP functions array_map, array_walk, the latter being just
a tiny bit quicker. Test script: (you may lower N to 12 or 11 on slower PC's)
<?php
set_time_limit(0); // it will take some time...
define("N", 13); // number of arrays
// calculate time in ms
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
// 4 different simple array modifiers:
function doSomethingByRef(& $a) {
for($i = 0; $i < sizeof($a); $i++) $a[$i]++;
}
function doSomethingByCopy($a) {
for($i = 0; $i < sizeof($a); $i++) $a[$i]++;
return $a;
}
function doSomethingWalk($i) {
$i++;
}
function doSomethingMap($i) {
return ++$i;
}
// end modifiers
// test function call N times and
// take time sample after each test:
function testFunctionCall($call) {
// setup N arrays of 2^N length
$b = array();
for($i = 0; $i < N; $i++) {
$b[$i] = array();
for($j = 0; $j < pow(2, $i); $j++) $b[$i][$j] = 0;
}
// call function on N arrays
$samples = array();
for($i = 0; $i < N; $i++) {
$a = $b[$i];
array_push($samples, microtime_float());
eval($call);
}
array_push($samples, microtime_float());
return $samples;
}
// run tests on 4 array modifiers:
$byRef = testFunctionCall("doSomethingByRef(\\$a);");
$byCpy = testFunctionCall("\\$a = doSomethingByCopy(\\$a);");
$byMap = testFunctionCall("\\$a = array_map(\\"doSomethingMap\\", \\$a);");
$byWlk = testFunctionCall("array_walk(\\$a,\\"doSomethingWalk\\");");
// output result:
echo "<pre>";
echo "Size\\tPassed array by Ref\\tReturn modified array\\t";
echo "Use array_map\\t\\tUse array_walk\\n";
echo "--------------------------------------------------";
echo "--------------------------------------------------\\n";
for($i = 1; $i < sizeof($byCpy); $i++) {
echo pow(2, $i - 1)."\\t";
echo str_pad($byRef[$i] - $byRef[$i-1], 20)."\\t";
echo str_pad($byCpy[$i] - $byCpy[$i-1], 20)."\\t";
echo str_pad($byMap[$i] - $byMap[$i-1], 20)."\\t";
echo str_pad($byWlk[$i] - $byWlk[$i-1], 20)."\\n";
}
?>
----
Manual Page -- http://www.php.net/manual/en/language.references.pass.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55648
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55648&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55648&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55648&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55648&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55648&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55648&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55648&report=yes
Search -- http://master.php.net/manage/user-notes.php