I’m making a 2d tile-based game. Some of my testing levels have few tiles, some have 500,000 for testing purposes. Running a performance profiler that comes with visual studio shows the bottleneck:
![the image must be here, it shows how <code>if (blocks[i].Visible)</code> takes 87% of time](https://i.stack.imgur.com/Wbb2t.png)
What is exact reason why it takes so much time? How do I avoid such situations?
UPD: nevermind, I’m just going through the whole array instead of going only through the ~200 visible tiles.
It’s not that evaluating each individual Visibility property/field would take much time – it’s that you are doing it 500k times – it adds up. Since this is what the profiler estimates is the bottleneck, it means that the vast majority of items have
Visibilityset to false – otherwise you would assume theDraw()method call would be shown as bottleneck. One approach to optimize this could be separating the visible items and only iterating over those.