Task Parallel is unstable, using 100% CPU at times

前端 未结 3 1757
甜味超标
甜味超标 2021-02-08 21:04

I\'m currently testing out Parallel for C#. Generally it works fine, and using parallel is faster than the normal foreach loops. However, at times (like 1 out of 5 times), my

3条回答
  •  旧时难觅i
    2021-02-08 21:37

    After some analysis, you're likely not even adding to these collections: 100,000,000 elements is still quite a bit smaller than the key search space (Approx. 2.1 billion), so these will likely not have any elements added, or just one or two.

    As for the particular issue, while I'm able to replicate it, I'm unable to give a direct answer as to why this is happening, but, I suspect it has to do with heavy contention around the memory bus in some way, and how it handles the partitioning and thread creation. Limiting the number of threads to the current processor count seems to help, but, it doesn't solve the issue entirely.

    All that said, a PLINQ version of things seems to be much faster and more consistent:

    var resultData = testData.AsParallel().Where(x => x == 1).ToList();
    

    Edit: It looks like this is a semi-obscure, but known issue, more details are available here: https://msdn.microsoft.com/en-us/library/dd560853(v=vs.110).aspx

提交回复
热议问题