Bug #68624 [NEW]: by reference arrays slow down within the function as the array grows
| From: | lindsay at notion dot co | Date: | Thu, 18 Dec 2014 22:51:19 +0000 |
| Subject: | Bug #68624 [NEW]: by reference arrays slow down within the function as the array grows | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-189117@lists.php.net to get a copy of this message | ||
From: lindsay at notion dot co
Operating system: linux and mac
PHP version: 5.6.3
Package: Performance problem
Bug Type: Bug
Bug description:by reference arrays slow down within the function as the array grows
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 bug report at https://bugs.php.net/bug.php?id=68624&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68624&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68624&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68624&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68624&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68624&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68624&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68624&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68624&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68624&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68624&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68624&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68624&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68624&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68624&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68624&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68624&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68624&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68624&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68624&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68624&r=mysqlcfg