0

This might be a very dumb question but it still bugs me. I tend to use this:

$variable = $someObject->getSomeValue();

for ($i = 0; $i < $x; $i++) {
   performSomeFunction($variable);
}

I have seen people do the following & some of my co-workers are arguing with me that there is no difference in performance if I use function calls within a loop & that it will save 1 line of code.

for ($i = 0; $i < $x; $i++) {
    performSomeFunction($someObject->getSomeValue());
}

Now for $x = 10 this might not have impact on performance but what if $x = 10000 ? Which one is the preferred way or best practice in PHP and in Programming general?

Example of function getSomeValue

// just a getter method for the class
function getSomeValue() {
   return $this->userFirstName;
}
6
  • Your co-workers are wrong. While $var = doSomething(); echo $var; is kind of silly, what you have in your loop is better. Unless, of course, getSomeValue depends on being in the loop. Commented Jun 26, 2014 at 14:58
  • @NiettheDarkAbsol no it does not depend. It will always be same for the iteration of the loop. Commented Jun 26, 2014 at 14:58
  • There may be a logical difference! Could the value every be modified in-between iterations...? Perhaps indirectly? Commented Jun 26, 2014 at 15:02
  • 1
    Performance would depend on how the method getSomeValue() works, but in any case I'd still go with your way over your coworkers, in case that method changes or you want to use a different value, or some other reason that you might not know yet. Your way is more readable, which is more important than having 1 less line of code. Commented Jun 26, 2014 at 15:02
  • My natural instinct, knowing nothing about the complexity of getSomeValue, would be to hoist the call out of the loop. However, if the function is a trivial getter, I would happily leave it inside because then the cost is negligible. Commented Jun 26, 2014 at 15:07

3 Answers 3

4

Highly depends on your function. For example this

function() {
  return 2;
}

It won't matter.

For this:

function() {
  $pdo->query('SELECT ....');
  // more db stuff
  return $someDbStuff;
}

It will matter extremely!

Sign up to request clarification or add additional context in comments.

3 Comments

In that case my co-workers might be right since all that function does is it returns one of the private variables of the class. Serves as a getter method
Your way is still good practice. Maybe the function gets changed in the future. Then noone will have your code in mind, and blow up the programm.
Thank you. Linking them to this comment :).
1

Depends on what you do on $someObject->getSomeValue();. If just returns a variable, it doesn't have any impact on performance, if, on the other hand, you are retrieving the data from a database, its very important to avoid it.

However, its always a good policy to avoid unnecessary iterations and do like this

$variable = $someObject->getSomeValue();

for ($i = 0; $i < $x; $i++) {
   performSomeFunction($variable);
}

Comments

1

Yeah, sure there's an impact in the performance. If you are about to use the same value over iterated loops, why would you go get it every time? It's better getting it before the loop (if there's no chance of changing this value while in the loop) and then reusing it.

Imagine this getSomeValue() needs to access a database or a webservice, would you rather do it 1 time or $x times for the same effect?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.