# Thread: which among the following two for loops is faster?

1. ## which among the following two for loops is faster?

please some body tell me which among these for loops is faster???

1)for(i = 0; i < 100; i++)
for(j = 0; i < 100; j++)
printf("first instance of for loop");

2) for(i = 0; i < 100; i++)
for(j = 0; j < 100; j++)
printf("second instance of for loop");

In the second for loop of first for loop instance the condition statement has variable 'i'.

So according to my knowledge of c first for loop istance is a infinite for loop. So second for loop instance execution is faster. If i am wrong please correct me and give the reason why my answer is wrong???

2. You are right to say that the first example enters an infinite loop while the second terminates. Since they result in different output, I am not sure if saying that one is faster than the other is really useful. For example, if the aim is to print the text at least 10000 times, then they would be expected to be equally fast in completing that task, but it so happens that the first does unnecessary work after completing the task.

3. So can we say that its application dependent....... as you said the first for loop instance does unnecessary work after completing the task then it still is consuming the cpu time and resources where as first for loop already had terminated. then at that moment we can say that second for loop is faster!!!!

4. Originally Posted by vapanchamukhi
then at that moment we can say that second for loop is faster!
That is like saying that Usain Bolt was the slowest of all the runners in the 100 metres sprint in the 2008 Olympic Games since he ran a victory lap after winning the race in world record time, and thus only stopped running after the other runners had stopped.

In other words, if you have a well defined goal, then you can reason and measure which algorithm and its implementation achieves the goal faster than another. Without a well defined goal, the notion of "faster" is meaningless, since you have not even addressed the issue of correctness.

5. The second loop finishes execution faster, because the first loop never finishes.
The first loop gets to 10001 faster, because the second loop never gets there.

It's a completely undefined question if you don't say what the application is.

6. both the for loops, i believe, takes same amount of time to reach the 10000th loop. at that perticular time second for loop terminates and the first one keeps on looping. i believe as the first one is looping infinite, second for loop is terminating after the 10000th loop. time taken to reach the 10000th loop is same but after that the second one stops so what to call at this perticular time.. as the first for loop gives the segmentation fault we can not use the first one in our application even if its faster as its going for infinite....

7. so its application dependent... so what is the answer if the question is which among the two for loop instances consumes less cpu resources. then i think second one is the answer.

8. So, if you have concluded that the first example is incorrect, then you can rule it out immediately.

Originally Posted by vapanchamukhi
so what is the answer if the question is which among the two for loop instances consumes less cpu resources. then i think second one is the answer.
I think so too, but what is the point of such a comparison when you have not established a well defined goal for the algorithm? You could just as well compare not doing anything to doing something.

9. i agree, because obviously you cannot compare the two of them-which is faster. Because the first loop doesn't have a goal to reach it's an infinite loop while the second loop has the goal to reach.

But let's say the two loops, loops correctly and they have the same goal to reach you can't still compare which of the loop below is faster: the first loop or the second loop? It's just the first set of loop will loop first before the second set of loop but it doesn't mean that the first loop is faster than the second loop-vice versa. There's no speed difference between.

Code:
```for( i = 0; i < 100; i++)
for( j = 0; j < 100; j++ )

for( k = 0; k <100; k++ )
for( l = 0; l < 100; l++ )```
I've heard but not quite sure that there's a speed difference between the two loops below. I'll leave it to the experts to answer that.

Code:
`for( x = 1; x <= 100; ++x )`
and
Code:
`for( x = 1; x <= 100; x++ )`

10. Originally Posted by \$l4xklynx
I've heard but not quite sure that there's a speed difference between the two loops below.
There may be a difference in efficiency in C++ if x is of a class type with overloaded prefix and postfix operator++ that have the same intended effect when called by themselves, but not in C.

11. i used to think that if some loop is an infinite loop then it consumes much of cpu resources. so i thought second one is the answer. but now here i got the answer that its application dependent. i corrected my doubt. but i have one more doubt. if the first for loop is faster can we use that some how in our application. i had this doubt. i am not writing any application using them.

12. Originally Posted by vapanchamukhi
i used to think that if some loop is an infinite loop then it consumes much of cpu resources. so i thought second one is the answer. but now here i got the answer that its application dependent. i corrected my doubt. but i have one more doubt. if the first for loop is faster can we use that some how in our application. i had this doubt. i am not writing any application using them.
The CPU resource used by loops [in general] is directly proportional to the number of iterations of the loop. In infinite loop will therefore consume an infinite amount of CPU resource. Of course, if there are blocking calls e.g. read/write from/to a device (such as a file, network socket or such) then this will make the loop run for a longer period of real time and potentially consume very little CPU time per real time unit (that means a low percentage of available CPU resource).

I don't think we can compare two loops that do not perform the same amount of workn (or at least COMPARABLE amount of work). If we were to compare cars for fuel economy, and say one car drives 200 km on 20 liters of fuel, and another car with an infinite supply of fuel (unknown as to what quantity that really is) has not yet stopped running - which is more efficient?

--
Mats