Edit report at http://bugs.php.net/bug.php?id=53242&edit=1
ID: 53242 Comment by: ceo at l-i-e dot com Reported by: einars at gmail dot com Summary: count() slow for global / static arrays Status: Bogus Type: Bug Package: Performance problem Operating System: Arch Linux PHP Version: 5.3.3 Block user comment: N Private report: N New Comment: Call me crazy, but once you declare it global in the function, having a local one seems kind of silly... Why not just have the one zval?... >From a guy who knows very little of php internals, but can't figure out the purpose of a "local" $items when it's been declared global in the function body. And "static" should be its own zval, not interacting in any way shape or form with any zval in any other scope... So it should have a refcount < 2 as well... Previous Comments: ------------------------------------------------------------------------ [2010-11-05 02:40:02] cataphr...@php.net To be expected. When you do global $items; the underlying zval has its refcount incremented to 2 since now it's referred to by the global symbol $items and the local symbol $items and the is_ref flag is set. Since sizeof does not receive its argument by reference and you're giving it an is_ref zval, a separation is forced and the value if copied. Since $items refers to a big variable (50k+ items), the performance penalty is big. To cut the story short: use $foo = $GLOBALS['items']. ------------------------------------------------------------------------ [2010-11-04 17:50:28] einars at gmail dot com Description: ------------ When the arrays are defined global or static, the sizeof() / count method suddenly gets very slow. Test script: --------------- <?php $items = range(0, 50000); $times = 200; $time = microtime(true); for ($i = 0; $i < $times; $i++) { $foo = sizeof($items); } printf("Non-global access: %.2fs\n", microtime(true) - $time); $time = microtime(true); for ($i = 0; $i < $times; $i++) { test_global(); } printf("Global access: %.2fs\n", microtime(true) - $time); function test_global() { global $items; $foo = sizeof($items); } Expected result: ---------------- % php array-test.php Non-global access: 0.00s Global access: 0.00s Actual result: -------------- % php array-test.php Non-global access: 0.00s Global access: 2.73s ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53242&edit=1