Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 C++ MFC
Which loop is fastest and best in c++ projects? and how?
Posted 21-Apr-13 22:53pm
Comments
pasztorpisti at 22-Apr-13 4:14am
   
The difference between different loop flow control constructs is logical, and not performance related. For example you can treat a while-loop as a simplified for loop without init-expression and post-loop-increment expression: for (; expr; ) {} is equivalent to while (expr) {}. In contrast to this do {} while (expr); can do something the previous to construct cannot (test at the end of the loop). You are basically comparing dogs and cats - which one is the better? It depends whether you want a shepherd for your sheep or catch mice... This comparison has nothing to do with performance. Anyway, if one of the loops were be better then the others probably wouldn't exist.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is not a 'best' loop in C++. There are three kinds of loop: for, while, do-while, each one with it pros and cons (forget about relative performance). In my own experience, the most used is for.
As a general rule to achieve good performance you have to
  1. Design (or use an existing) good algorithm.
  2. Master the programming language.
 
Proceeding with point (2) you'll notice that 'fastest loop' questions make almost no sense.
  Permalink  
Comments
gurunbr1 at 22-Apr-13 4:26am
   
fastest means i'm desiging a game project, so i execute a single code with multiple times so i need a fastest execution.
Coder Block at 22-Apr-13 7:34am
   
whatever,you design game project or any thing else,its not depend on which loop you are using its totally depend what logic your applying.That gives you faster execution.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The best loop is no loop at all ( search loop unrolling ). If you want to do something several times and very fast then consider doing it in parallel using multiple cores or even using SSE extended assembler instructions. Don't try to do large chunks of your program this way just the small expensive bits of mathematics that it's suitable for.
 
In general optimization is a mugs game and you should not do it until everything else is done if at all but in certain applications, video processing and no doubt rendering for a game project it can be beneficial.
 
Almost all worthwhile optimizations are a tradeoff, for example use more memory to get more speed or take longer but use less memory. You need to decide what's important and what you're prepared to trade for it in terms of speed, memory, complexity and coding effort.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Some things to consider:
 
First, the speed of a loop usually depends on the code performed within much more than the type of loop.
 
Second, which loop to use depends on the way you wish to control the loop. You can rewrite any type of loop as another, but that won't change your requirements and efforts for fulfilling them! E. g. if you know you need to run a loop 376 times, then you need a loop counter, whether or not you use a for loop. OTOH, if your loop gets reiterated until a value exceeds a certain limit, a while or do loop makes more sense than for.
 
Third, you don't even know there will be a bottleneck. Maybe the code runs faster than the user can perceive, no matter how you program it? It is wasted time and effort to start optimizing before you even know there is a problem that requires it!
 
Fourth, tweaking the code just for making it run faster may be self-defeating: modern compilers are extraordinarily good at recognizing code patterns and optimizing them. But if you deliberately tweak you code, that may prevent the compiler from performing these optimizations!
 
TL;DR
Don't try to optimize until you've written the code, noticed a performance problem, and pinpointed the cause of the bottleneck!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 215
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 159
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100