I would like to iterate through a List of a custom class with some typical properties. The list will be huge and growing(starts with 3000items and may reach +10k)
I am reading about best ways to iterate in a very fast way and checking properties, because I shall fill some counters.
In .NET, which loop runs faster, 'for' or 'foreach'?
http://www.codearsenal.net/2013/12/csharp-multithreading-loop-parallel-for.html#.U09-qPl_vA0
But if I need to follow a logic checking these properties... what would be faster?
At the moment my code looks like this:
public void CalculateTotalCounters()
{
#region ParallelCounting
countTotal = 0;
countMapped = 0;
countNotMapped = 0;
countError = 0;
errorFound = false;
MyList.AsParallel().ForAll(acc =>
{
lock (this)
++countTotal;
if (!string.IsNullOrEmpty(acc.isMapped))
{
lock (this)
++countMapped;
}
if (string.IsNullOrEmpty(acc.isMapped))
{
lock (this)
++countNotMapped;
}
if (acc.HasError || acc.NotUnique || acc.DefalutName || acc.DefaultValue)
{
lock (this)
{
++countError;
errorFound = true;
}
}
});
#endregion
}
I know two of the conditions for isMapped could be in just one if-else(Might this affect the lock?)
Thank you.