[UPHPU] performance hit recalling functions?

Michael J. I. Jackson mjijackson at gmail.com
Sun Dec 20 09:35:06 MST 2009


When in doubt, check the source. ;) You can find the definition of count() in ext/standard/array.c on line 300 (in the 5.3.1 source). Since count() can work on multiple types of objects, you'll see that it performs different functions based on what kind of object you give it. In the case of an array it calls php_count_recursive (a C function). Looking at the source to that function (which starts on line 268 in the same file), you can see that it merely calls zend_hash_num_elements (defined in Zend/zend_hash.c on line 1013). Looking at that function, you'll see that it merely returns the internal nNumOfElements property of the array (actually a HashTable after the call to Z_ARRVAL_P) object. So, in a sense it is cached. However, you still have to jump through all of the C code to get to the value you're really looking for (which is, obviously, extremely fast).

So, your question really could be rephrased as "Is it faster to call 3 C functions which then return a static cached value or to store the value in a userland variable and then look it up in the current symbol table?" Without running any tests, I would tend to say that you'd be better off storing the value in a user variable and then using that because variable lookups should be very fast. But that's just my guess.

Michael

On Dec 18, 2009, at 11:38 PM, Wade Preston Shearer wrote:

> If you are going to be calling a function (such as count() a few times  
> within a script, is there any performance benefit to calling it once  
> at the beginning, storing the result in a value and then using the  
> value instead of recalling the function each time?
> 
> _______________________________________________
> 
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net



More information about the UPHPU mailing list