One other thought on optimisation: you will be tempted to believe that arrays are better than List<T>'s, and that for loops are better than foreach. Neither of these things are true.
The List<T> class is heavily optimised and often performs better than an array. If you are returning a list of items from a method purely for iteration, use IEnumerable<T> as the return type and the yield keyword in the method:
C# code: public IEnumerable<Vector3> GetVectors()
{
Vector3 v;
while ((v = MakeVector()) != null)
{
yield return v;
}
}
...
foreach (Vector3 v in GetVectors())
{
// do something
if (condition) break;
}
The C# compiler optimises the shit out of this kind of thing. It's co-processed, so each call to MakeVector only happens as you iterate through the foreach loop. If you break out of the foreach, the IEnumerable is destroyed, so you don't make unnecessary calls to MakeVector, which you might if you populated an entire array in advance. Also, if you implemented parallelism at some point in the future, the operation could be split across multiple cores.Twinkle, twinkle, little star, I don't wonder what you are:
You're the cooling down of gases, forming into solid masses. |