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

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    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. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, if you have concluded that the first example is incorrect, then you can rule it out immediately.

    Quote 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.
    Last edited by laserlight; 02-11-2009 at 11:25 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    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++ )
    Last edited by $l4xklynx; 02-11-2009 at 11:27 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    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. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vapanchamukhi View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Faster bitwise operator
    By Yarin in forum C++ Programming
    Replies: 18
    Last Post: 04-29-2009, 01:56 PM
  2. Faster way of printing to the screen
    By cacophonix in forum C Programming
    Replies: 16
    Last Post: 02-04-2009, 01:18 PM
  3. Computations - which is faster?
    By ulillillia in forum C Programming
    Replies: 9
    Last Post: 12-09-2006, 10:23 PM
  4. does const make functions faster?
    By MathFan in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2005, 09:03 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM