I'm making a XNA game and I wonder if there is a way of optimizing some loops. For example:
I have a Map class, that contains a collection of tiles, so, in the Map Update() I just call every tiles Update()
// Update method in Map Class
public void Update()
{
for (int index = 0; index < tiles.Count; index++)
{
tiles[index].Update();
}
}
This works fine but, it can get worst with some larger populated objects, like the Particle class where, every particle is managed with a ParticleManager class (that contains a collection of particles) so:
// Update method in ParticleManager class
public void Update()
{
for (int index = 0; index < particle.Count; index++)
{
particle[index].Update();
}
}
//Update Method in Particle class
public void Update()
{
for (int index = 0; index < Map.tiles.Count; index++)
{
CheckCollitions(Map.tile[index], this);
}
}
The ParticleManager loops for every particle and, each particle checks collitions for every Tile. So, if you got 20 particles and 100 Tiles, it will do lots of computation:
20 loops cycles * 100 loops cycles
That's why I was thinking of some optimizations, like loop unrolling but, I don't know if it works (I think not) with undefined length loops (cause the compiler doesn't know those loops lengths in compile time)
To sum up:
- It is possible to optimize those loops using loop unrolling?
- Can you advice me with any other type of optimization?
Thanks