As has been said, the for loop will most likely be more performant, but you can still clean the code up a bit more:
for (int i = 0; i < fpc.Vertices.Length; i++)
{
if (cylindre.IsPointInside(fpc.Vertices[i]))
listPoint.Add(fpc.Vertices[i]);
}
Can become:
foreach (var vertice in fpc.Vertices)
{
if (cylindre.IsPointInside(vertice))
listPoint.Add(vertice);
}
EDIT: As per the performance question. This code will run in LINQPad. I found that the foreach version performs a few milliseconds better than the for loop.
var elements = Enumerable.Range(0, 4000000).Select(x => true).ToArray();
var sw = new Stopwatch();
var result = new List<bool>();
int trueCount = 0;
sw.Start();
for(int i=0; i < elements.Length; ++i)
{
if (elements[i])
{
++trueCount;
result.Add(elements[i]);
}
}
sw.Stop();
sw.ElapsedMilliseconds.Dump();
sw.Reset();
trueCount = 0;
result = new List<bool>();
sw.Start();
foreach(var element in elements)
{
if (element)
{
++trueCount;
result.Add(element);
}
}
sw.Stop();
sw.ElapsedMilliseconds.Dump();
EDIT2 - Regarding the link that seems to indicate such drastically poor performance in a foreach loop
The link (http://www.schnieds.com/2009/03/linq-vs-foreach-vs-for-loop-performance.html) is dealing with removing elements from a list. In the case that you have a generic list, removing elements via a for loop is not normally the best approach. A better approach is to use the RemoveAll method. RemoveAll will remove all elements matching the predicate and then consolidate the list as opposed to RemoveAt which will require moving all elements above the removed element. For performance comparison of the removal please see the below code (once again, this can be run in LINQPad). In my testing the RemoveAll ran roughly 250x faster.
var elements = Enumerable.Range(0, 100000).Select(x => x % 2 == 0).ToArray();
var sw = new Stopwatch();
var source = new List<bool>(elements);
sw.Start();
for(int i=0; i < source.Count; ++i)
{
if (!source[i])
{
source.RemoveAt(i);
--i;
}
}
sw.Stop();
sw.ElapsedMilliseconds.Dump();
int count = source.Count;
sw.Reset();
source = new List<bool>(elements);
sw.Start();
source.RemoveAll((bool x) => {return x;});
sw.Stop();
sw.ElapsedMilliseconds.Dump();
AddRange()
instead offoreach
. It won't increase performance, but it will make your code more readable. Also, usingWhere()
directly will be shorter thanfrom
/where
/select
. \$\endgroup\$