-2

Is there any performance difference between for, while or do...While loop in c?

One of my professor told me for loop was faster when scoldering me for never using them (for readability, I think for loop are less readable than (do...)while loop).

I don t have a compiler on hand nor time to test execution time, and if there is a difference, I would like to know why.

(I know a lot of SO question seems similar, but are either asking about while vs do while, or haven t any answer)

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • 3
    There is no performance difference but there is a functional difference that is sometimes important. `for` and `while` loops have the opportunity to never run if their exit condition is immediately met. `do...while` will run at least once. – simonc Dec 23 '13 at 14:31
  • 3
    "Is there any performance difference between for, while or do...While loop in c?" - ***NO.*** –  Dec 23 '13 at 14:31
  • 3
    This question appears to be off-topic because it is hypothetical. –  Dec 23 '13 at 14:32
  • Check the [selected answer](http://stackoverflow.com/questions/5518027/performance-of-c-loops-for-vs-while-vs-goto?rq=1) here – Suvarna Pattayil Dec 23 '13 at 14:33
  • I suppose a `for`-loop is slightly more predictable in the number of iterations but any ½-decent (or even ¼-decent) compiler will optimize the difference away. – Kninnug Dec 23 '13 at 14:33
  • Or [this](http://stackoverflow.com/questions/20172402/do-compilers-produce-better-code-for-do-while-loops-versus-other-types-of-loops/20172586#20172586). –  Dec 23 '13 at 14:34
  • 1
    @haccks (If there is *any* proof, at all... :P) –  Dec 23 '13 at 14:35
  • @haccks: Well, he told me he would check again, because his claim was based on now old compiler, I don t have anymore details about which compiler, but it i suseful to know. – DrakaSAN Dec 23 '13 at 14:54
  • @DrakaSAN; I don't have much idea about old compilers. May be he is right. – haccks Dec 23 '13 at 14:56
  • @haccks: Well, I don t really care about old compiler, I don t think I ll ever use one, and just want to take good habits while still being learning. If for was indeed faster than while, I should have use it, since there is no difference, I ll stick with while loops. – DrakaSAN Dec 23 '13 at 15:08
  • you can compare their asm output: `gcc -S mycsource.c` – Yousha Aleayoub Mar 28 '18 at 13:24

1 Answers1

5

The overall semantics of all three iteration statements is the same once they have been compiled into binary code.

They just provide a different taste over the same thing. Performance depends on:

  • the complexity of the body of the loop
  • the complexity of calculating the end condition / next iteration step

Since they all provide both of them there is no performance difference per se in any of them. Unless you consider small irrelevant things that you shouldn't care in any case.

There could be some optimization tricks that can be done according to the kind of loop but you shouldn't rely on them, even because they could be compiler dependent, so meaningless from your point of view.

Jack
  • 131,802
  • 30
  • 241
  • 343