-3

I need to call a function getCount($stage) multiple times (say 15, since $stage can have values from 0 to 14). Which method is faster:

for ($i=0; $i<15; $i++)
{
  getCount($i);
}

OR

getCount(0);
getCount(1);
.
.
.
getCount(14);

?

The current page I'm working on has similar scenario. It uses the second method and it lacks readability. I'm looking forward to change it to the first method. Will there be any advantage or disadvantage in the aspect of runtime? Which method will be faster?

Anoop S
  • 141
  • 1
  • 12
  • why not check your self ? http://stackoverflow.com/a/9288945/1723893 – NullPoiиteя Jan 09 '15 at 13:00
  • Why dont you test it?, its not like you cant or something, but to me it seems easier to do the first method, since you dont have to copy paste your function then edit the parameter. –  Jan 09 '15 at 13:01
  • 1
    If this is time sensitive it will be inside getCount() where most of the run spends its time; any difference in looping vs inlining the invokation will be completely trivial – Alex K. Jan 09 '15 at 13:02
  • Why not pass an array to getCount() and then loop it through. Not so much a performance gainer but it surely looks more clean. – Peter Jan 09 '15 at 13:03
  • 1
    Not only is this is a matter of "freaking try it", but more important why the hell would you care – PeeHaa Jan 09 '15 at 13:04
  • @NullPoiиteя I checked myself. I can't detect any changes in both methods (maybe because the loop is small or the function is not that much complex?). I don't know which method will be faster in case of some complex functions. That's why I asked this question. – Anoop S Jan 09 '15 at 13:04
  • @PeeHaa As you are saying. It might differ a gazzilionth of a second. – Peter Jan 09 '15 at 13:05
  • 1
    Then the correct answer is "is the same". – Riccardo Cagnasso Jan 09 '15 at 13:07
  • I would aim for the most readable and maintainable solution, whichever suits you best. – AnotherGuy Jan 09 '15 at 13:13

3 Answers3

1

I wouldn't be concerned about using a loop for this. You will get two main benefits of using the for loop.

  1. You can dynamically change the number of times the function is called since the number of times isn't hard-coded.
  2. You get more maintainable code which is easy to identify what its doing.

Performance wise when you are using language constructs like the for loop the performance hit is minimal. So I would prefer easier to read and more maintainable code over a very, very small performance increase (if its even noticeable by a client)

Regards.

AnotherGuy
  • 605
  • 11
  • 20
0

Theorically The first method should be slower because there's some added check and variable. Consider that it's difficoult to exactly know what happens inside the php intepreter and what kind of optimizations are in place, so this might even not be true, but this is not much important because...

Pratically It's the same, the performance hit of a loop is so minimal that if you are not doing some kind of extreme high performanche computing stuff (in php?!) both approach are completely equivalent speed-wise.

-1

They are both similar. One just takes up more lines of code.

Nathan Farren
  • 23
  • 1
  • 7