Thread: How to waste CPU cycles in a program?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    How to waste CPU cycles in a program?

    I want the program to spend some time running but 'for ( ; ; )' is not suitable because I do not want it to run forever. I want something that I can kind of control how long it runs. That is, I want something like
    Code:
    for (i; i<1000; i++)
      x = x + i;
    and then I can adjust the maximum loop index (1000, 10000, 100000, ..) depending on the actual run. But apparently the compiler optimizes away the loop though I use the -O0 flag with gcc.
    What is an effective and efficient way to do this?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The method you're trying to use for adding delays is a horrible one for 2 reasons: 1) The speed of the computer will affect the amount of delay, 2) You're chewing up valuable CPU time that could be better used for other applications while yours is sleeping.

    A better method is to use your platform-specific function for putting your program to sleep for a certain amount of time. In Windows try the Sleep() function.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    I know I am chewing up CPU resources, that's why the title is "How to WASTE CPU cycles in a program?" I do not mind that. I also know the speed of the computer will affect the amount of delay, that's why I need to adjust the max loop index from one run to another, just so that the time the program spends in the loop will approximately be what I want.
    I do not want sleep() because I am testing a program involving Unix signals. sleep() sets the SIGALRM signal and thus affects the testing of the signal handler, at least on Solaris.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by hzmonte View Post
    I want the program to spend some time running but 'for ( ; ; )' is not suitable because I do not want it to run forever. I want something that I can kind of control how long it runs. That is, I want something like
    Code:
    for (i; i<1000; i++)
      x = x + i;
    and then I can adjust the maximum loop index (1000, 10000, 100000, ..) depending on the actual run. But apparently the compiler optimizes away the loop though I use the -O0 flag with gcc.
    What is an effective and efficient way to do this?
    Call gettimeofday() in a tight loop, breaking the loop when the time has reached whatever time you wanted. This meets the "wasting CPU" requirement and doesn't require sleep() or signals.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,290
    > What is an effective and efficient way to do this?
    volatile int i;
    should do the trick.

    Notwithstanding all the other problems with the technique.
    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.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by Salem View Post
    Notwithstanding all the other problems with the technique.
    What problems? Why is "the solution must busy loop the CPU" an invalid requirement?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by brewbuck View Post
    What problems? Why is "the solution must busy loop the CPU" an invalid requirement?
    Nothing per se, except I don't feel like using that program unless there's a serious reason for it.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,608
    A for loop will not run the same speeds on all systems. You need a timer and the timer resolution to do this correctly.

    1. Set a variable to 0 outside of the loop.
    2. Increase the variable by the timer difference inside the loop.
    3. When the variable reaches a certain delay value (as chosen by you), exit the delay loop and continue with the program then goto 1.

    Code:
    UINT uTotalTime=0;
    UINT uLastTimer=<some_func_to_get_timer_value>;
    
    do
    {
       uTotalTime+=<some_func_to_get_timer_value>-uLastTimer;
       if (uTotalTime><some_delay_value>)
       {
          uTotalTime=0;
          uLastTimer=<some_func_to_get_timer_value>;
          .....continue with code
       }
    } while (<some_condition>);

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,290
    > I do not want sleep() because I am testing a program involving Unix signals.
    An alternative is using select() with empty FD_SETs and the timeout set to what you want to sleep for.
    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.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    How about putting the solution(s) in the FAQ "How to ...?"

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,290
    > What problems? Why is "the solution must busy loop the CPU" an invalid requirement?
    But that wasn't the underlying question, which was how to introduce a delay without using a signal generating sleep() call.

    Besides, if you used a busy delay loop on a mobile device, you'd get your ears chewed off by the battery life brigade.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM