문제

Do the parallel methods Parallel.For and Parallel.ForEach,

internally provide their own load balancing and partitioning to perform better?

If so and what circumstances do you want to write create your own Partitions using the Partitioner class to improve performance?

도움이 되었습니까?

해결책

The routines do provided their own partitioning.

They are based on "typical" scenarios, but can occasionally require guidance, particularly in unusual situations.

For example, the default partitioning for IEnumerable<T> implementations (that don't implement IList<T>) will start with a small group per task, and slowly grow in size. However, if you know your IEnumerable<T> is going to slowly feed one item at a time, this will cause a block, as the Parallel class will "wait" on the next item until it receives enough elements for a partition and schedules it.

By providing your own partitioner, you can prevent this, and get better throughput.

Another great example of where a custom partitioner helps is if you have very small amounts of work per loop item. In this case, partitioning yourself and working on the partition avoids unnecessary overhead. This is covered in the How to: Speed Up Small Loop Bodies page on MSDN.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top