Thread: No-opp loop

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    No-opp loop

    I am having trouble with a simple no-opp loop on linux
    Code:
    for (int i=0 ; i < 999999 ; i++) {}
    when compiled with gcc, the loop is ignored (unles I have something in its block)

    Anyway to avoid this behavior ??

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    turn off optimization? (-O0)

    also, are you sure the loop is ignored? on modern computers 999999 iterations is nothing.

    try
    Code:
            for (int i = 0; i < 3e8; ++i) { }
    runs for just over 1 second on my 2ghz Core 2 Duo.
    Last edited by cyberfish; 12-21-2007 at 12:12 AM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    loop is ignored, bec when I run the prog (it is very short) it finishes instantly, but when I print i it takes some time to complete.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    printing calls a OS function that is taking up most of the time

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    cyberfish@cyberfish-laptop:/tmp$ echo "int main() { for (int i = 0; i < 3e8; ++i ) ; }" > a.cpp
    cyberfish@cyberfish-laptop:/tmp$ g++ a.cpp
    cyberfish@cyberfish-laptop:/tmp$ time ./a.out
    
    real    0m1.225s
    user    0m1.200s
    sys     0m0.012s

  6. #6
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    runs for just over 1 second on my 2ghz Core 2 Duo.
    - nice
    I am runing linux over a virtual machine on vista on P4 2.66 so it should be slower.

    Anyway I fixed the problem like this:
    Code:
    for (int i=0 ; i < 999999 ; i++)
          for (int x=0 ; x < 9999 ; x++) {};

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    that is more or less the same as
    Code:
    for (int i=0 ; i < 999999*9999 ; i++);

  8. #8
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Very nice trick, did not know about that.

    Thanks !

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    you didn't know how to multiply two numbers together?

    embedding the second for loop in the original multiplies the iterations of the outer by the inner.

    Do you understand that the only reason it doesn't delay before finishing is because a modern computer can compute over 2 billion instructions a second?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A better way would be to declare i as being volatile. This will at least continue to do the same thing at various optimisation levels.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    But what's the point of doing nothing 999999 times?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    On modern processors, it's a fine grained way of modifying the fan rotation speeds
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    On modern computers, it's a nice way of eating up the cpu for useless tasks. Use an API to delay instead. I don't know about any apis for linux though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    On modern computers, it's a nice way of eating up the cpu for useless tasks. Use an API to delay instead. I don't know about any apis for linux though.
    Yes, in Linux there is a wonderful little function called "usleep" that "waits" for the same amount of time whatever processor you have, whether it's a slow processor, or a fast one, and you specify the number of microseconds, so 1000000 (1 million) will take 1 second.


    Using "timing with empty loops" is very much dependant on how fast the processor makes the loop and how clever the compiler is - but I believe at least some time back that gcc was actually NOT optimizing away empty loops entirely, but it would potentially move things out of the loop, if the loop content was "unchanging", e.g.
    Code:
    for(i = 0; i < 10000; i++)
       x = sin(1);
    would become:
    Code:
    x = sin(1);
    for(i = 0; i < 10000; i++)
        ;
    This can make a big difference!

    [Oh, and it may also unroll the loop FIRST, then stuff things outside, so the loop may count up by 4 or 8 at a time, I think].

    And I'm pretty sure that microsofts compilers will do even more than that.

    In a Windows application, the function "Sleep" will allow the application to "sleep" for a specified number of milliseconds.

    Footnote: Although the time is specified in milliseconds or microseconds, neither Linux nor Windows are what is called "Real Time OS's", so the precision of any timing is very dependant on what else is going on in the system. The precision that you can expect is something in the region of 10ms or worse - which is perfectly fine for human waiting times, but if you want to make perfect notes by tweaking the pins connected to a speaker, you'll be a bit out of tune now and again. This, however, is no different if you were to use any other timing method, because for precise timing, you NEED a real-time OS.

    --
    Mats
    Last edited by matsp; 12-21-2007 at 06:58 AM. Reason: Fix code-tag.
    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.

  15. #15
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    In general, a do-nothing loop is a bad thing on on a PC.* It requres 100% CPU time to do nothing, and the multitasking operating system will take cycles away from your other programs in order to allocate time to your do-nothing loop. Your program becomes a "hog", slowing down any other programs that are running. (A few milliseconds usually won't hurt anything, although it might foul-up something like real-time audio/video.)

    When you use Sleep() it sleeps your program, freeing-up the CPU time for other programs. Sleep() is not Standard ANSI/ISO C++, but it's best to use a non-standard function in this situation.


    * The operating system does run a do-nothing loop when the CUP is "idle", but this is the lowest priority task. And, it's OK (usually it's required) to use do-nothing loops in an embedded system, where you have full control of all running programs and you are in charge of allocating CPU cycles.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM