2

I am trying to learn about concurrency and to do so I have been writing some programs. I have noticed, as I expected, that wall time does generally decrease when adding threads. However, I have also noticed that CPU time increases as the number of parallel tasks I run increases.

Is it because when there's only a single thread the compiler behind the scenes optimizes the code so to use more than one core (if possible)? This is the only explanation I came up with. Can anyone help me?

EDIT: the machine I use has multiple cores (4 to be precise)

fresh
  • 381
  • 2
  • 11
  • maybe this can help you https://stackoverflow.com/questions/47703/multiple-threads-and-performance-on-a-single-cpu – arana Nov 22 '19 at 16:41

2 Answers2

5

The CPU time is the time CPU spends executing instructions. This does not include wait times for I/O. If you have a single processor/core, then the wall time is CPU time plus I/O time.

For multiple cores, CPU time is measured as the total CPU time for all cores. So if you have a program with multiple threads, and if those threads are scheduled on multiple cores, the CPU time will increase, but wall time may decrease. If you have a multi-threaded program that relies on CPU and doesn't to I/O, then the CPU time will be close to wall time * number of cores.

Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
2

Imagine you were trying to do two things, one at a time. You'd be pretty relaxed as you were doing each thing. But it would take you awhile.

Now imagine that you're trying to do two things at the same time. Presumably, it would take you less total time because sometimes you could make forward progress on both things and sometimes when you were stuck on one you could work on the other and vice versa. But your total effort would probably be greater as you spent more time moving between tasks.

The same is true of your CPU.

Of course using multiple threads will make some things faster than doing things purely sequentially. If not, we wouldn't get much benefit from them at all. But sometimes the threads waste time fighting over who gets to adjust the process' memory table or one uses memory bandwidth that slows the other down.

It's quite typical to see multi-threading reduce the real time it takes to finish a set of tasks but increase the total resource utilization required.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Re, "...two things at the same time." How about a concrete analogy: Suppose you are tasked with doing the laundry, and cooking dinner. After you've started the washing machine, will you just sit and watch it until the cycle is finished? Or can you go to the kitchen and start preparing food _concurrently_ with the laundry task? And when the casserole goes into the oven, will you just sit and watch it bake? or can you go to the laundry room and fold clothes? A person waiting for the washer or the oven is analogous to a program waiting for I/O. – Solomon Slow Nov 22 '19 at 19:55