Edit report at http://bugs.php.net/bug.php?id=53242&edit=1
ID: 53242 Updated by: [email protected] Reported by: einars at gmail dot com Summary: count() slow for global / static arrays -Status: Open +Status: Bogus Type: Bug Package: Performance problem Operating System: Arch Linux PHP Version: 5.3.3 Block user comment: N New Comment: 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']. Previous Comments: ------------------------------------------------------------------------ [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
