Reputation: 159
I am trying to split collection into equal number of batches.below is the code.
public static List<List<T>> SplitIntoBatches<T>(List<T> collection, int size)
{
var chunks = new List<List<T>>();
var count = 0;
var temp = new List<T>();
foreach (var element in collection)
{
if (count++ == size)
{
chunks.Add(temp);
temp = new List<T>();
count = 1;
}
temp.Add(element);
}
chunks.Add(temp);
return chunks;
}
can we do it using Parallel.ForEach()
for better performance as we have around 1 Million items in the list?
Thanks!
Upvotes: 2
Views: 1276
Reputation: 1062520
If performance is the concern, my thoughts (in increasing order of impact):
temp = new List<T>(thisChunkSize)
new T[thisChunkSize]
BlockCopy
(or CopyTo
, which uses that internally) rather than copying individual elements one by oneArraySegment<T>
would help; if you're open to using newer .NET features, this is a perfect fit for Memory<T>
/Span<T>
- creating memory/span ranges over an existing array is essentially free and instant - i.e. take a T[]
and return List<Memory<T>>
or similar.Even if you can't switch to ArraySegment<T>
/ Memory<T>
etc, returning something like that could still be used - i.e. List<ListSegment<T>>
where ListSegment<T>
is something like:
readonly struct ListSegment<T> { // like ArraySegment<T>, but for List<T>
public List<T> List {get;}
public int Offset {get;}
public int Count {get;}
}
and have your code work with ListSegment<T>
by processing the Offset
and Count
appropriately.
Upvotes: 4