i am trying to benchmark For and Parallel For for copying a list into an individual list
here Points are struct of int x , int y
Below is the benchmark code :
var points = addPoints();
int nbPoints = points.Count;
Measure("Normal Forloop", () =>
{
List<int> x = new List<int>(nbPoints);
List<int> y = new List<int>(nbPoints);
for (int i = 0; i < nbPoints; i++)
{
x.Add(points[i].X);
y.Add(points[i].Y);
}
});
Measure("Parallel Forloop", () =>
{
ConcurrentBag<int> x = new ConcurrentBag<int>();
ConcurrentBag<int> y = new ConcurrentBag<int>();
Parallel.For(0, nbPoints, i =>
{
x.Add(points[i].X);
y.Add(points[i].Y);
});
});
Now for a list size of NumberOfPoints = 1000000;
performance of normal for loop : 24 ms
performance of Parallel For loop : 367 ms
why did the parallel for loose so badly , is it because of concurrentbags ?