A XNA game I was making was running slowly on my WP7 device. However when I started it in Windows Phone Performance Analysis to find the bottleneck, the game ran smoothly on the same device.
I figured the problem out a little later, and it was an adjacency-checking function. I have optimized the function and the game works fine now.
Why was the game magically speeding up in Perf Analysis mode? Any possible explanations?
This was the code that ran faster on the Perf Analysis mode, but slow otherwise. It is a damage-dealing function in a tower defense game.
foreach (Target myTarget in _targets)
{
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0) continue;
try
{
Tile adjacent = _tiles[(int)myTarget.CurrentTile.X + x, (int)myTarget.CurrentTile.Y + y];
if (adjacent.Tower != null)
{
DealDamage(myTarget, adjacent.Tower);
}
}
catch
{
}
}
}
}
I optimized it later by checking damage for each tower instead of each target.
