khtan (58) [Avatar] Offline
#1
In MEAP v2, Ch4, Fig 4.5, the graph shows that the Elapsed time (blue) for C# Parallel to be less than C# Sequential, due to the effects of parallelism, as expected. This shows that the algorithm is 3x faster.

However, the CPU time (brown) shows increasing values for C# Parallel vs C# Parallel.For. What does this mean? and what is contributing to it?
Riccardo Terrell (16) [Avatar] Offline
#2

Good question, I will provide a better explanation and I’ll update the book.


The CPU time is the time for which the CPU was doing some work executing a given task. On the other side, the elapsed time is the total clock time that the operation took to complete regardless of any resource delays or parallel execution.
In general, the elapsed time is higher than CPU time, however, this value changes in a multicore machine.
When a concurrent program runs in a multicore machine, we achieve true parallelism. In this case, the CPU time becomes the sum of all the execution times for each thread running in a different CPU at same give time. For example, in a 4 cores computer, when you run a single threaded (sequential) program, the elapsed time is equal (almost) to CPU time because there is only one core working. Instead, running the same program in parallel leveraging all the 4 cores, the elapsed time becomes lower, because the program run faster, but the CPU time increases because it is calculated by the sum of the execution time of all 4 parallel threads. Thus, when a program uses more than one CPU to complete the task, the CPU time may be more than the elapsed time.


Does this answer make sense?
khtan (58) [Avatar] Offline
#3
Thanks.

I would summarize your explanation as :

The CPU time means how much time the program takes with all the parallelism going on
while the Elsapsed time is measuring how much time all the threads are taking, and
ignoring the fact that the threads overlap when running in parallel.