Jump to content

PHP Caching?


mikesmith1287

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.