-1

I am currently creating a socket server in PHP and I would like to know which one would be faster to use throughout it. I've heard for loops are faster than while loops, but I don't know about do whiles.

Thanks

raina77ow
  • 103,633
  • 15
  • 192
  • 229
Tybone Ten
  • 29
  • 1
  • 2

4 Answers4

3

Depending on the benchmark you use, do loops have been shown to be marginally faster:

http://www.ebrueggeman.com/blog/php_benchmarking_loops

Test            Avg Execution Time  
for loop        23.44 ms
while loop      24.65 ms
do while loop   22.96 ms

In this benchmark, foreach was shown to out-perform other loop types, and while was found to out-perform for. I bring this up because you will note that this contradicts the first test I cite.

Finally, this benchmark supports the findings of the first, that while is marginally faster than for.

The conclusion? No benchmark is capable of emulating your use case to the extent that you should base your decision on it, and this may be a micro-optimization that won't materially improve your program. Use the statement that fits the situation, then write tests to benchmark your own application using variations. It is pretty trivial to swap the while and the for -- try them both and see what you get. Then unlearn that lesson for your next project, because it will be unique and again deserve individual testing.

Documentation

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
1

If you're so concerned with speed of your code, profile it. Whatever you read about the specific code snippets' performance (in places like this, for example), might turn wrong in your specific case - because of some weird PHP interpreter quirk, or something.

There's a plenty of profiling tools available for PHP programmer - starting from a simple microtime, ending with complete profiler toolkits like XDebug. I suggest reading this topic for basic guidelines of profiling.

Community
  • 1
  • 1
raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

There is no difference per se as they're basically just different ways of writing loops, directly taken from C.

The only thing that can be impacting is the number of times a condition is evaluated but you can count it yourself.

So use what is the most concise and readable.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • They're always very bad benchmarks being produced on blogs on this point and of course no conclusion is never found, because there is so small difference in assembly you can't measure it in a real high level program. – Denys Séguret Jun 21 '12 at 06:18
  • Right, but the statement "There is no measurable difference" is not scientifically true. There IS a difference, and it IS measurable. Whether it is enough to care about is another matter entirely. You edited that word out, good call. :) – Chris Baker Jun 21 '12 at 06:21
  • If you refer to my answer, you will find that I am in agreement with you in practice. However, it is indeed theoretically possible to isolate and compare the differences in execution time of the various control statements. One could, for example, reduce the clock speed by a known percentage, then run empty loops and compare the time differences in ratio to the slow down. Just saying... "no measurable difference" is overreaching. Without that word, we are in agreement. – Chris Baker Jun 21 '12 at 06:27
0

Use whatever fits your purpose. for loops and while loops are used to loop with the condition checked first. do-whiles guarantee that the code is run at least once.

Thus you can immediately narrow down your choices to either for/while or do-while based on this constraint. Trying to use an inappropriate construct would result in very ugly code which most likely be slower anyways.

Between for and while, I don't think there's any difference. Even if you were writing a scientific library, this would barely be noticeable (if at all). The general guideline is: for loops are designed for iterating through things and while loops are used as other general purpose looping.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140