Why does a busy loop often uses 100% of the cpu time while loops that implement complex algorithms would use much much less?
thanks :)

- 12,395
- 3
- 34
- 49

- 5,006
- 17
- 69
- 106
-
sleep once in a while... – ismail May 01 '11 at 19:36
-
I know, but why do we need to sleep? (not as humans but as mach...ines? O_o) – Idov May 01 '11 at 19:37
5 Answers
JUMP instructions in the CPU architecture are inefficient because they cause the pipeline to flush. A busy loop is effectively an infinite series of JUMP instructions.

- 12,395
- 3
- 34
- 49
-
4This is why i always unroll my busy loops, for better performance. – Tom Anderson May 01 '11 at 19:52
-
1On the other hand, the branch prediction in the CPU works really well for unconditional jumps. – Greg Hewgill May 01 '11 at 19:56
A complex algorithm can certainly use 100% of the cpu. However, many loops that implement complex algorithms either explicitly yield the thread periodically and/or have some code that calls down into the OS at some point, where either the thread is yielded or something that requires a wait (such as calling on a co-processor) happens.

- 232,168
- 48
- 399
- 521
First, if your busy loop uses 100% then you aren't doing it right. Sleep for a bit.
Second, complex algorithms often involve memory to store values as opposed to just looping. Any time the thread needs to use an external resource like memory, disk, etc. the CPU has to wait a bit. This is why you would see it use less than 100%.

- 3,012
- 2
- 20
- 22
-
Wow! lots of answers while I typed this. I need to go back to my Mavis Beacon typing software! – Richard Brightwell May 01 '11 at 19:44
-
I would say that if your busy loop *doesn't* use 100% of your CPU then you aren't doing it right. That is what a busy loop is. It is essential to its nature. I think what you're getting at is that a busy loop is generally not what you want, which is a fine point. – Tom Anderson May 01 '11 at 19:51
-
1I wasn't sure what the accepted definition for busy loop was, so I looked at [What is a busy loop?](http://stackoverflow.com/questions/4911397/what-is-a-busy-loop) It seemed the most accepted answer was a polling loop. I did write a program to warm my sandwiches which did a 100% busy loop, so I can see a use for that too. – Richard Brightwell May 01 '11 at 20:01
-
Interesting. I've always understood 'busy wait' to mean something like 'without any suspension of execution', so not including a poll, sleep, or yield. Perhaps it depends on context - i mostly come across busy waits in the really low-level threading algorithms, like the implementation of a semaphore (where you go around a tight loop with a CAS instruction until you've acquired the lock). There, there's no way to poll, sleep, or yield, because this is the code that's implementing polling, sleeping, and yielding! – Tom Anderson May 01 '11 at 20:32
-
1And yes, i've dried a pair of socks by iterated modular multiplication of bigints, so there is definitely a thermal use too. – Tom Anderson May 01 '11 at 20:32
A "busy loop" does not have to talk to memory, so the CPU is basically doing all work itself without waiting for external input.

- 90,431
- 16
- 141
- 175
Depends what that "complex algorithm" is doing. Does it do Hard Drive access? Network requests? Interact with any other hardware? When that happens the CPU will have to wait for those things to complete, so it sits around doing nothing (or does a context switch to some other work) while it waits for that information to come back.

- 138,677
- 31
- 291
- 286