Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

I use often the function sizeof($var) on my web application, and I'd like to know if is better (in resources term) store this value in a new variable and use this one, or if it's better call/use every time that function; or maybe is indifferent :)

like image 637
markzzz Avatar asked Apr 19 '11 13:04

markzzz


People also ask

What happens if you call a function with a return value without storing or using the returned value?

Yes, You can call a function but never store the returned value by that function in java. But it will just unnecessarily consume processing time. Because you will not be achieving what you want to achieve by first declaring that you want some processing to be done by method and return a final output.

How do you store functions in a variable?

Functions stored in variables do not need function names. They are always invoked (called) using the variable name. The function above ends with a semicolon because it is a part of an executable statement.

Can a variable name be used multiple times?

As long as the variables maintain what they mean in the context of the code, you can re-assign them all you want.


4 Answers

TLDR: it's better to set a variable, calling sizeof() only once. (IMO)


I ran some tests on the looping 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 items:

A) took 0.0085 seconds
B) took 0.0049 seconds

With a array of 180 items:

A) took 0.0078 seconds
B) took 0.0043 seconds

With a array of 3600 items:

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

Although there isn't much of a difference, you can see that as the array grows, the difference becomes more and more. I think this has made me re-think my opinion, and say that from now on, I'll be setting the variable pre-loop.

Storing a PHP integer takes 68 bytes of memory. This is a small enough amount, that I think I'd rather worry about processing time than memory space.

like image 65
Dave Avatar answered Oct 04 '22 09:10

Dave


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

In the example you suggested, the difference in processing code produced by this approach and the alternative (repeatedly calling the function) would be insignificant. However, where the function in question is more complex it would be better to avoid executing it repeatedly.

For example:

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

Executes in 0.225273 seconds on my server, while:

$date = date('Y-m-d');

for($i=0; $i<10000; $i++) {
  echo $date;
}

executes in 0.134742 seconds. I know these snippets aren't quite equivalent, but you get the idea. Over many page loads by many users over many months or years, even a difference of this size can be significant. If we were to use some complex function, serious scalability issues could be introduced.

A main advantage of not assigning a return value to a variable is that you need one less line of code. In PHP, we can commonly do our assignment at the same time as invoking our function:

$sql = "SELECT...";

if(!$query = mysql_query($sql))...

...although this is sometimes discouraged for readability reasons.

In my view for the sake of consistency assigning return values to variables is broadly the better approach, even when performing simple functions.

like image 43
cantlin Avatar answered Oct 04 '22 09:10

cantlin


If you are calling the function over and over, it is probably best to keep this info in a variable. That way the server doesn't have to keep processing the answer, it just looks it up. If the result is likely to change, however, it will be best to keep running the function.

like image 39
Mild Fuzz Avatar answered Oct 04 '22 07:10

Mild Fuzz


Since you allocate a new variable, this will take a tiny bit more memory. But it might make your code a tiny bit more faster.

The troubles it bring, could be big. For example, if you include another file that applies the same trick, and both store the size in a var $sizeof, bad things might happen. Strange bugs, that happen when you don't expect it. Or you forget to add global $sizeof in your function.

There are so many possible bugs you introduce, for what? Since the speed gain is likely not measurable, I don't think it's worth it.

like image 39
Ishtar Avatar answered Oct 04 '22 07:10

Ishtar