How can I achieve maximum parallelism and utilize maximum CPU with Parallel.ForEach?

后端 未结 1 1342
无人及你
无人及你 2021-02-08 23:09

There is a C# function A(arg1, arg2) which needs to be called lots of times. To do this fastest, I am using parallel programming.

Take the example of the fo

1条回答
  •  盖世英雄少女心
    2021-02-08 23:16

    You have determined that the code in A is the problem.

    There is one very common problem: Garbage collection. Configure your application in app.config to use the concurrent server GC. The Workstation GC tends to serialize execution. The effect is severe.

    If this is not the problem pause the debugger a few times and look at the Debug -> Parallel Stacks window. There, you can see what your threads are doing. Look for common resources and contention. For example if you find many thread waiting for a lock that's your problem.

    Another nice debugging technique is commenting out code. Once the scalability limit disappears you know what code caused it.

    0 讨论(0)
提交回复
热议问题