14

Recently I was surfing on Stack Overflow (Python) and saw this post where Aaron Hall claims that

constantly running while loops can consume a lot of processing power. Adding a sleep period (even only a second) can greatly reduce that usage.

Is it really true? And if so, how come? Does the same rule apply to other programming languages as well (i.e. C++)?

Community
  • 1
  • 1
SnuKies
  • 1,578
  • 1
  • 16
  • 37
  • 2
    Adding sleep in a loop normally means there is a problem with the program logic but yes it does help bad code. – NathanOliver Jul 18 '16 at 13:15
  • What if you want to keep a program running forever while it wait for an interrupt? Is there a better alternative? – dashiell Jul 18 '16 at 13:22
  • 1
    If the while loop is there just to wait for some external event, it needs to be replaced by some sort of multithread tool designed to wait for the event. If it's polling the disk for a file, then sleep then check is probably the best option. But simply burning through actual cpu instructions like while (true) is about the worst possible way to wait for the event. Is there a better way ... depends on what the event is that you are waiting for. – Kenny Ostrom Jul 18 '16 at 13:36
  • If you're waiting for an interrupt your wait call should be blocking, which means your executing thread doesn't continue executing (consuming CPU) until the blocking call returns. See blocking I/O on this topic. – Simon Pirschel Jul 18 '16 at 13:38
  • 1
    I just read the linked question. The event he's waiting for is literally for a specified amount of time to pass, which is exactly what time.sleep is supposed to do. – Kenny Ostrom Jul 18 '16 at 13:40
  • "even only a second". You should know a second is an eternity for a CPU. – Sven Nilsson Jul 18 '16 at 14:21
  • @KennyOstrom If you're waiting on file events you should use inotify or similar https://en.wikipedia.org/wiki/Inotify – Falmarri Jul 18 '16 at 18:50

1 Answers1

20

TL;DR If you are polling for an event that happens once a minute, you might want to not check every nano second.

Yes, it is really true. Sleeping in a thread does reduce the CPU usage of that thread. While a thread sleeps, it hardly consumes any CPU time.

Yes, this is true for largely any language as long as the sleeping is implemented using the OS native threading API.

To think about this intuitively, let us consider a simplified version of the program from the linked question:

while end_condition_expression:
    if time_based_condition_expression:
        do_something()

Now, let us simplify the world, and assume that the program is the only process running on the processor.

Let us also assume, that the time based condition is true once a minute. Let us also assume that executing end_condition_expression and time_based_condition_expression costs 10 nano seconds of cpu time.

How much cpu time will this consume within a minute? Exactly one minute == 60 000 000 000 nano seconds. The cpu usage will have been 100% during the entire time. The loop will have iterated six billion times.

Now, consider this variation of the program:

while end_condition_expression:
    if time_based_condition_expression:
        do_something()
    sleep_for_a_second()

How many times will the loop have executed within a minute? 60 iterations. How much cpu time will this consume within a minute? 60 * 10 ns = 600 ns. That is one hundred millionth of what the non-sleeping version of the program used.

In reality, there is a bit of overhead from the call to sleep, and the cpu time is shared with other processes, and a scheduler is involved, and the exact cpu usage won't exactly match my assumptions, but the idea stays the same.

eerorika
  • 232,697
  • 12
  • 197
  • 326