Answer the question
In order to leave comments, you need to log in
How to correctly calculate the execution time of a multithreaded algorithm?
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
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.
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 questionAsk a Question
731 491 924 answers to any question