English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

6 answers

I believe the cost of the loop (besides the actual contents) is determined more by whatever you are using as the exit comparison. If you are using an integer value, it can compare faster than say a string value. For example:

If the exit condition for either the for or while loop is:
x==1

That is processed faster than if you are doing something like:

strcmp("blah",x)==0

2007-02-05 05:27:38 · answer #1 · answered by Amanda H 6 · 0 0

linearly, a while loop would execute faster. This is my answer if I answer dumbly, meaning, literally look at the loops. Both for loop and the while loop iterate through a block of code. The while loop however, ONLY checks if a condition is true to determine if the block of code is to be reiterated. The for loop, checks a condition and generally increments an iterator before iterating through a block of code. This means technically two things happen before a for loop is iterated where as in a while loop only one. However, because the for loop (generally) uses an iterator, the code to execute the for loop may indeed be faster than a while loop with a more complex evaluation. For example:

for(int i=0; i < 10; i ++)
{
// Some code
}

while (aString != StringFromFunction())
{
// Some code
}

in this case, an integer requires 4 bytes of memory. To perform the comparison, The compiler will compile the code down to pulling the integer into two registers in the processor, and performing a compare and if less than jump to the beginning of the block to execute. The while loop on the other hand will require a push of all the variables onto the stack, a register setup for the function to be called and then a jump to the starting address of the function. The function then executes, returns a value. The original register values are popped off the stack. That value then has to be evaluated against the lvalue (in the statement i == 5, i is the lvalue and 5 is the rvalue). Since the data is a string, the comparision will require another implicit call to a function. Another push to stack, and function execution. You get the picture....a for loop and a while loop with an identical block of code to execute will execute equally fast. But the speed difference will be what code is executed to perform the evaluation and the increment of the iterator (in the for loop). Because of the very object-oriented nature of C++, so much code can be compiled implicitly by the compiler to make a seemly innocous statement like i= 3 compile. Even in procedural C, the speed of one loop verses the other depends on what is executed for the evaluation. So I guess my final answer is (IT DEPENDS!!) but, especially with today's compilers built with optimizing technology, you should be more concerned about with statement is the natural best choice as a solution to a particular problem. Some iterations naturally align themselves with the while loop while others are better constructed using a for loop.

2007-02-05 08:22:26 · answer #2 · answered by Tarie N 3 · 0 0

For Loops and while loops are really just conveniences for the programmer. A for loop can be converted into a while loop and while into a for. In the eyes of the compiler, they are structurally the same. It's just a different syntax for the same thing.

When it gets compiled, they will be compiled into the same thing...basically just a conditional jump. For example:

int i = 0;
while(i < 10) i++;

will be compiled into the assembly:

xor eax, eax
WhileLoopStart:
inc eax
cmp eax, 0x0a
jl WhileLoopStart

This code:

int i = 0;
for(i = 0; i < 10; i++);

will be compiled into:

xor eax, eax
ForLoopStart:
inc eax
cmp eax, 0x0a
jl ForLoopStart


Of course, this is assuming that the two loops are exactly equivalent. The programmer testing of exit conditions could end up making the biggest difference, depending on the programmer.
Also, alot of this has to do with the compiler. The compiler should produce the same code for both, but you never know with extremely complex loops. Compiler optimizations could also have an impact, but I no expert on optimization theory. That's a subject onto itself.

2007-02-05 06:04:31 · answer #3 · answered by Anonymous · 0 0

It depends. A for loop has 3 variables.

1. Set the initial value - x=1
2. Test value - x>y
3. Increment xalue - x++

If y=2, then the loop runs once. If y=100, then it runs more.

Same with a while loop. It might not run at all if the conditions are not met or it might run a million times.

2007-02-05 05:21:38 · answer #4 · answered by Yuchniuk Website Design 3 · 0 0

Sorry. you mustn't strengthen such an impressions. it relatively is an persons determination to apply i, j as loop administration variables. it relatively relies upon upon the loop shape. for(i=0;i

2016-10-01 11:26:37 · answer #5 · answered by Anonymous · 0 0

Depends on how your particular compiler/linker has translated your code into machine language. I would guess that they would be the same, if the compiler optimizes the code correctly.

2007-02-05 05:22:59 · answer #6 · answered by rongee_59 6 · 1 0

fedest.com, questions and answers