Click here to Skip to main content
13,045,285 members (59,396 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Which loop is fastest and best in c++ projects? and how?
Posted 21-Apr-13 21:53pm
pasztorpisti 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
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.
gurunbr1 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 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
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.
Rate this: bad
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!

Don't try to optimize until you've written the code, noticed a performance problem, and pinpointed the cause of the bottleneck!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2017
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