php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68624 by reference arrays slow down within the function as the array grows
Submitted: 2014-12-18 22:51 UTC Modified: 2014-12-18 23:12 UTC
From: lindsay at notion dot co Assigned: nikic (profile)
Status: Closed Package: Performance problem
PHP Version: 5.6.3 OS: linux and mac
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: lindsay at notion dot co
New email:
PHP Version: OS:

 

 [2014-12-18 22:51 UTC] 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)) {}
	}
}



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-18 22:55 UTC] 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 23:12 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2014-12-18 23:12 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Mar 14 00:01:32 2025 UTC