mikesmith1287 Posted October 20, 2010 Share Posted October 20, 2010 Hello, I just had a quick question about caching when the compiler compiles php into assembly (or however this works with php). Anyways, is there any efficiency in doing this: $holder = strlen($anArray); for (i=0 to 100) echo $holder; Rather than this: for (i=0 to 100) echo strlen($anArray); ? If PHP is smart, then strlen($anArray) would be called once, and any subsequent calls to strlen($anArray) would just call a value stored in a cache. Is this how PHP works? Quote Link to comment https://forums.phpfreaks.com/topic/216413-php-caching/ Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 No. PHP assumes that you want the strlen() each iteration because that's what you told it. What if you modify $anArray in the loop? You would want the new value yes? FWIW, if $anArray is really an array, then use count() instead of strlen(). strlen() will always issue a notice and return 5 on an array (5 characters in the word 'Array') and strlen() expects a string. Quote Link to comment https://forums.phpfreaks.com/topic/216413-php-caching/#findComment-1124634 Share on other sites More sharing options...
mikesmith1287 Posted October 20, 2010 Author Share Posted October 20, 2010 Sorry, for some reason I was thinking array (since a string is a type of array), but I mean to type '$aString'. But let's just say it's an array because it's more generic. Anyway, couldn't the cache simply assume that the array will not be changed in the loop, and if it is changed in the loop, then the compiler can assume that it will be changed each time. In either case, if the compiler guesses wrong, then the compiler would have to wait, but overall the compiler would run faster this way, no? If PHP doesn't run this way, then it's nice to know that if I am going to access the same indice in an array over and over, i should use a "holder" variable. Quote Link to comment https://forums.phpfreaks.com/topic/216413-php-caching/#findComment-1124667 Share on other sites More sharing options...
mikesmith1287 Posted October 20, 2010 Author Share Posted October 20, 2010 Actually, I think it would not be faster at all now that I think about it, it would just be a smarter, higher level compiler. I guess I am just a bit used to higher level languages at the moment. Should I assume everything in PHP works at the C-level? Quote Link to comment https://forums.phpfreaks.com/topic/216413-php-caching/#findComment-1124669 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 Php does perform some short-circuit optimization like what you are suggesting, but I doubt it does it in the case of a value returned by a function call. This would be something that you would need to devise a test for and determine yourself what if any optimization it is performing for any particular case. Quote Link to comment https://forums.phpfreaks.com/topic/216413-php-caching/#findComment-1124672 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.