Bug #68624 [Opn->Csd]: by reference arrays slow down within the function as the array grows
| From: | nikic@php.net | Date: | Thu, 18 Dec 2014 23:12:28 +0000 |
| Subject: | Bug #68624 [Opn->Csd]: by reference arrays slow down within the function as the array grows | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189119@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68624&edit=1
ID: 68624
Updated by: nikic@php.net
Reported by: lindsay at notion dot co
Summary: by reference arrays slow down within the function as
the array grows
-Status: Open
+Status: Closed
Type: Bug
Package: Performance problem
Operating System: linux and mac
PHP Version: 5.6.3
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This was a limitation of PHP 5.x COW design, which disallowed sharing of values between referenced
and non-referenced zvals. This problem will no longer exist in PHP 7.
Previous Comments:
------------------------------------------------------------------------
[2014-12-18 22:55:12] lindsay at notion dot co
And my function name of walk_array is misleading. is_array gets slow which probably doesn't
walk the array.
------------------------------------------------------------------------
[2014-12-18 22:51:18] lindsay at notion dot co
Description:
------------
Within a function, if an array is passed in by reference, operations on the array get slow within
the function as the array grows. In this example, is_array() gets much slower. Here is output from
the example script with and with out the array variable being passed as reference.
walk_array
10000 0.039294004440308
20000 0.031054973602295
30000 0.026264190673828
40000 0.025592803955078
50000 0.026119947433472
60000 0.025648832321167
70000 0.026489973068237
80000 0.023653984069824
90000 0.028777122497559
walk_array_by_reference
10000 1.7504909038544
20000 6.3624141216278
30000 14.600469827652
40000 28.24251294136
50000 37.910901069641
60000 46.824372053146
70000 60.680932044983
80000 71.826647043228
90000 78.522189855576
Tested against 'PHP 5.5.14-2+deb.sury.org~precise+1 (cli)' and 'PHP 5.6.2 (cli)'
Test script:
---------------
<?php
define('LOOP_COUNT', 100000);
define('LOOP_PRINT', 10000);
$array = [];
walk_array($array);
walk_array_by_reference($array);
function walk_array($array)
{
echo 'walk_array' . PHP_EOL;
$timer = microtime(true);
for ($i = 1; $i < LOOP_COUNT; $i++)
{
if ($i % LOOP_PRINT == 0)
{
echo $i . ' ' . (microtime(true) - $timer) . PHP_EOL;
$timer = microtime(true);
}
$array[$i] = 1;
if (is_array($array)) {}
}
}
function walk_array_by_reference(&$array)
{
echo 'walk_array_by_reference' . PHP_EOL;
$timer = microtime(true);
for ($i = 1; $i < LOOP_COUNT; $i++)
{
if ($i % LOOP_PRINT == 0)
{
echo $i . ' ' . (microtime(true) - $timer) . PHP_EOL;
$timer = microtime(true);
}
$array[$i] = 1;
if (is_array($array)) {}
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68624&edit=1