Is PHP's count() function O(1) or O(n) for arrays? -


does count() count elements of php array, or value cached somewhere , gets retrieved?

well, can @ source:

/ext/standard/array.c

php_function(count) calls php_count_recursive(), in turn calls zend_hash_num_elements() non-recursive array, implemented way:

zend_api int zend_hash_num_elements(const hashtable *ht) {     is_consistent(ht);      return ht->nnumofelements; } 

so can see, it's o(1) $mode = count_normal.


Comments