PHP count() is O(1)
Avishkar Autar · Oct 10 2013 · PHP
I was curious about the performance of PHP’s count() function a while back and whether it was worth it to store the result in a variable for repeated use. I discovered the following from this answer by FractalizeR on Stack Overflow:
PHP_FUNCTION(count) calls php_count_recursive(), which in turn calls zend_hash_num_elements() for non-recursive array, which is implemented this way:
ZEND_API int zend_hash_num_elements(const HashTable *ht)
So you can see, it’s O(1) for $mode = COUNT_NORMAL.