Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top