views:

124

answers:

3

What's the best in terms of speed and performance? To call the function each time you need the value (e.g mysql_num_rows, time) or to copy the return value to a local variable and use that instead.

Example: Let's say that I call the time() function ten times to get the current time, would it be faster to use a local variable those ten times instead?

+11  A: 

This basically comes down to:

Does this make your code more readable? This should be your first consideration.

How expensive is the function call? Don't micro-optimize. Like if each call gets 2000 records from a database or calls a remote Web service then yes the performance of that call is significant. But a lot of the time it isn't.

Is it appropriate to store the value? By this I mean take time(), it can change between calls. This may be what you want or you may want to use the same time at several points, in which case you'd store it.

cletus
+1  A: 

Variables are definitely faster.

too much php
A: 

Profile, profile, profile.

Geerad