|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-12-19 19:19 UTC] anon at anon dot anon
[2013-12-20 03:22 UTC] laruence@php.net
-Status: Open
+Status: Not a bug
[2013-12-20 03:22 UTC] laruence@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 17 05:00:01 2025 UTC |
Description: ------------ The is_array() function does not takes it's argument by reference, but copies it. When it takes a large array as an argument and it is called this way many times (for example in a "for" cycle) then it can take a huge time to run beacause of large amount of memory copies. The same problem probably exists at other array related functions. In PHP 5.3 and earlier you cound use is_array(&$array) to mitigate the performance bottleneck. Since call-time pass-by-reference was removed in PHP 5.4 there is no way to do a workaround anymore. Test script: --------------- <?php function delta($prefix, $t1) { $t2 = microtime(true); printf("%s %0.6f\n", $prefix, ($t2 - $t1) * 1000); } function &genarray() { $a = array(); for($i = 0; $i < 10000; $i++) { $a[] = $i; } return($a); } function noref(&$a) { for($i = 0; $i < 10000; $i++) { is_array($a); } } function byref(&$a) { for($i = 0; $i < 10000; $i++) { is_array(&$a); } } $a = &genarray(); $t1 = microtime(true); noref($a); delta("NoRef:", $t1); $t3 = microtime(true); byref($a); delta("ByRef:", $t3); ?> Expected result: ---------------- I expected that NoRef timing be (almost) equal to ByRef timing. Actual result: -------------- NoRef: 9246.673107 ByRef: 1.910925 NoRef is ~5000 times slower than ByRef.