C# Parallel Vs. Threaded code performance

前端 未结 4 1123
耶瑟儿~
耶瑟儿~ 2021-02-14 03:19

I\'ve been testing the performance of System.Threading.Parallel vs a Threading and I\'m surprised to see Parallel taking longer to finish tasks than threading. I\'m sure it\'s d

4条回答
  •  情书的邮戳
    2021-02-14 04:18

    The comparison is not very fair in regard to Threading.Parallel. You tell your custom thread pool that it'll need 10 threads. Threading.Parallel does not know how much threads it will need so it tries to adapt at run-time taking into account such things as current CPU load and other things. Since the number of iterations in the test is small enough you can this number of threads adaption penalty. Providing the same hint for Threading.Parallel will make it run much faster:

    
    int workerThreads;
    int completionPortThreads;
    ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads);
    ThreadPool.SetMinThreads(10, completionPortThreads);
    
    

提交回复
热议问题