I got a large list to loop through (1.500.000 items), with each item I have to do a very small check. Totally during 30 seconds.
The CPU utilization when using Sequential is around 10%, so there's a lot of resources not used.
The first thought was to use Parallel, but due to the limited time duration for each item, Parallel lasts longer than a sequential Foreach, this is due to "Why was the parallel version slower than the sequential version in this example?", which explains that the creation of each task will cost time.
So I had another thought and that is to divide the list in 4 (or more) equal peaces and create a thread to loop through the items to get it faster.
Before creating my own class, is this a good approach? Or any other thoughts of how to speed things up? Or do you know a better way of handling this.
Code
The code I created for another parallel approach: (used in my own static class)
public static void ForEach<T>(IEnumerable<T> list, Action<T> body, int listDevide)
{
// Number of items
int items = list.Count();
// Divided (in int, so floored)
int listPart = items / listDevide;
// Get numbers extra for last run
int rest = items % listDevide;
// List to save the actions
var actions = new List<Action>();
for(var x = 0; x < listDevide; x++)
{
// Create the actions
actions.Add(delegate {
foreach(var item in list.Skip(x * listPart).Take(listPart))
{
body.Invoke(item);
}
});
}
// Run the actions parallel
Parallel.Invoke(actions.ToArray());
}
Remark: "rest" variable to do the last items is currently not used in this example.
Solution below, more info: http://msdn.microsoft.com/en-us/library/dd997411.aspx