multithreadingconcurrency

Does multithreading increase CPU time?


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)


Solution

  • 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.