Thread: Sleep() causing unexplained reduction in cpu load

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    6

    Sleep() causing unexplained reduction in cpu load

    I have written a test code using timer delegates. About 30 methods are delegated and will be called on the expiry of a timer. This causes the cpu load to spike up every 2 seconds. While trying to optimize this code, i added a "sleep(29)" just before delegating it. The result shouldn't be any different as the timeout gets shifted by 29ms for all the timeouts. But this has cut down the cpu load by 50%. I am quite happy with the result but couldn't find any logical explanation for the reduction in cpu load.Can someone please explain why this is happening?

    Thanx in advance...

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by sajanphilip View Post
    But this has cut down the cpu load by 50%. I am quite happy with the result but couldn't find any logical explanation for the reduction in cpu load.Can someone please explain why this is happening?
    If I'm not mistaken it happens because the timer is taxing the cpu. The sleep simply puts the thread... well, to sleep. When the timer resumes, it sees it needs to fire and does its thing.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Your CPU load maybe 100&#37; but I bet you if you ran another application while you were doing that, it would run decently. When you tell a thread to go to sleep, and specify a short sleep duration, and the processor has nothing better to do, it will wake up the thread and tell it to get back to work. But if the CPU has something better ( or equally as good) to do, then sometimes the sleep will be longer than the specified time because sleep basically means "Hey, I got nothing to do for at least #, let someone else have a turn"

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    Thanx guys. I was trying out the same thing using a simple code. Executing it each time took different amounts of time.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also bear in mind that if you spread a load (e.g. adding sleeps), it will perhaps use the same amount of CPU time overall [if not more], but it will be over a longer timeframe. This only matters if there is other work to be done. Using 100% CPU is not a problem if it happens only some of the time - it's only a problem if the CPU is loaded to 100% for extended periods.

    --
    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. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. How can I load a cpu a certain percentage?
    By Smartiepants586 in forum C Programming
    Replies: 4
    Last Post: 06-07-2005, 12:15 PM
  3. Cpu Load
    By Fric in forum C Programming
    Replies: 9
    Last Post: 03-28-2005, 06:26 PM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  5. retrieving current CPU load
    By kristy in forum C Programming
    Replies: 2
    Last Post: 11-30-2003, 09:30 AM