D
D
Dmitry Gavrilenko2014-11-20 19:57:34
.NET
Dmitry Gavrilenko, 2014-11-20 19:57:34

How to correctly calculate the execution time of a multithreaded algorithm?

17b88d611cfc49cf931cdc047905957e.PNG
Here is a simple code. In fact, 4 threads almost evenly split the array into equal parts, and each of the threads, in its range, performs a 5-fold increase in all array values ​​by 2.
As you can see, each thread has its own Stopwatch instance. The Times() method calculates the average of the execution time of all threads and prints to the console.
Question: Why is the average execution time of threads about 940 ms, while the real execution time is about 5 seconds.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
aush, 2014-11-20
@Maddox

Your threads are not running at the same time. Take Join out of the loop.

S
Sergey, 2014-11-20
Protko @Fesor

Well, you don't think that all 4 processes work all the time, do you? If you run your application with a higher CPU priority, theoretically the difference will be slightly less. But it all depends on how many processor cores are available to you. Also, do not forget that you are not the only one who takes up processor time on the entire computer.

T
tsarevfs, 2014-11-20
@tsarevfs

It seems to me that you create the next thread only after the previous one has ended. As a result, instead of parallel execution, sequential execution is obtained, and the time is summed up.
Thread.Join "Blocks the calling thread until the thread terminates or the specified time elapses..."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question