0

Is writing the a statement itself multiple times better than using a loop, if the iterations are small in number (<=5)? Basically, what I want to ask is does using loops effect the execution time and memory consumed for a code? Speaking in terms of memory, it appears that while loop does not consume any extra memory, whereas for loop uses only one extra variable. And, I don't really know about the difference in execution time.

So, are there any benefits of not using loops?

anshabhi
  • 423
  • 6
  • 29
  • 6
    Your compiler will unroll the loop if it thinks it will be faster. You can safely assume that it is better than you at doing this. Write readable code. – Alexandre C. Jun 13 '15 at 13:32
  • The compiler will do some [loop unrolling](https://en.wikipedia.org/wiki/Loop_unrolling) for you automatically. You also should not care about the memory some loop counters need. Just write what is most readable until you can prove that it hurts your performance. – Baum mit Augen Jun 13 '15 at 13:32
  • Other comments are good, but the point is - *It won't make a difference in performance!*. This is being *Penny Wise - Pound Foolish* (and you are in good company). In other words, if speed is what you want, learn how to get it. You don't get it by guessing. [*This is the method I and many others use.*](http://stackoverflow.com/a/378024/23771) – Mike Dunlavey Jun 13 '15 at 13:46

0 Answers0