Is it better to call a function every time or store this value in a new variable?

I often use the sizeof($var) function in my web application, and I would like to know whether (in terms of resources) it is better to store this value in a new variable and use this, or whether it is better to call / use every time this function; or maybe it doesn't matter :)

+6
source share
7 answers

I tested some tests on the loop aspect of this small array:

 $myArray = array("bill", "dave", "alex", "tom", "fred", "smith", "etc", "etc", "etc"); // A) for($i=0; $i<10000; $i++) { echo sizeof($myArray); } // B) $sizeof = sizeof($myArray); for($i=0; $i<10000; $i++) { echo $sizeof; } 

With an array of 9 elements:

 A) took 0.0085 seconds B) took 0.0049 seconds 

With an array of 180 elements:

 A) took 0.0078 seconds B) took 0.0043 seconds 

With an array of 3600 elements:

 A) took 0.5-0.6 seconds B) took 0.35-0.5 seconds 

Although there is not much difference, you can see that as the array grows, the difference becomes more and more. I think that made me think about my opinion and say that from now on I will set a variable precycle.

PHP integer storage occupies 68 bytes of memory. This is small enough, I think I'd rather be worried about processing time than memory space.

+3
source

In general, it is preferable to assign the result of a function that you are likely to repeat with a variable.

In the example that you suggested, the difference in the processing code generated by this approach and the alternative (multiple function calls) would be insignificant. However, when the function in question is more complex, it would be better not to re-execute it.

For instance:

 for($i=0; $i<10000; $i++) { echo date('Ym-d'); } 

It runs in 0.225273 seconds on my server, and:

 $date = date('Ym-d'); for($i=0; $i<10000; $i++) { echo $date; } 

executes 0.134742 seconds. I know that these fragments are not quite equivalent, but you understood this idea. For many months or years, many pages are loaded by many users, even a difference of this size can be significant. If we were to use some kind of complex function, serious scalability problems could be introduced.

The main advantage of not assigning a return value to a variable is that you need another line of code. In PHP, we can usually complete our task at the same time as calling our function:

 $sql = "SELECT..."; if(!$query = mysql_query($sql))... 

... although this is sometimes discouraging for reasons of readability.

In my opinion, for consistency, assigning return values ​​to variables is generally the best approach, even when performing simple functions.

+3
source

If you call the function over and over, it is probably best to store this information in a variable. Thus, the server should not continue to process the response, it just looks. However, if the result is likely to change, it will be better to continue working.

+1
source

Since you are assigning a new variable, it will take a little more memory. But it can make your code a little faster.

The troubles he brings can be great. For example, if you include another file that uses the same trick, and both save the size in var $sizeof , bad things can happen. Strange mistakes that happen when you do not expect this. Or you forget to add global $sizeof to your function.

There are so many possible errors that you enter, for what? Since speed gain is probably not measurable, I don't think it's worth it.

+1
source

If you don't call this function a million times, your "performance boost" will be negligible.

0
source

I do not think this is really important. In a sense, you do not want to repeat the same thing over and over, but considering that it is sizeof(); if it is not a massive array, you should be fine.

0
source

I think you should avoid constructs like:

 for ($i = 0; $i < sizeof($array), $i += 1) { // do stuff } 

For sizeof, each iteration will be performed, even if it often does not change.

While in constructions like this:

 while(sizeof($array) > 0) { if ($someCondition) { $entry = array_pop($array); } } 

You often have no choice but to compute it at each iteration.

0
source

Source: https://habr.com/ru/post/886240/


All Articles