Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
I don't have the knowledge to help, but maybe adding the code before and after your fix would help others. –  George Duckett Apr 19 '12 at 14:56
    
Just to be clear, the same code consistently ran faster while I ran it in Perf Analysis mode. I don't understand the reasons behind this happening. The fact that I optimized the code later is irrelevant. –  ApoorvaJ Apr 19 '12 at 18:41
1  
It might not be irrelevant, it sounds like the code you changed was fast in Per Analysis mode and not normally. Maybe you could isolate it. So if you posted before and after, the differences might be what the Performance Analysis mode does better (for whatever reason). –  George Duckett Apr 19 '12 at 19:56
    
@GeorgeDuckett: Valid point. I'm adding the code. –  ApoorvaJ Apr 21 '12 at 9:27
2  
Perhaps is a caching issue. Maybe the perfomance read the same variables as this code, but trigger some caching mechanism, so the data of the map is always on memory. –  Tei Apr 27 '12 at 12:50
show 3 more comments

2 Answers

My guess is the try/catch overhead for each iteration of a potentially large nested loop was killing your performance and that the analysis tool somehow affected the exception handler overhead. You could easily test that by removing the exception handling and running normally, without the profiler.

You definitely don't want a try/catch on the inside of a tight loop. You would be much better off ensuring that you account for anything that can generate an exception within your loop via conditional checks, or at the very least put the try/catch around the loop as a whole.

share|improve this answer
    
Will give it a try, though I suspect this behavior has got something to do with caching. –  ApoorvaJ Jul 14 '12 at 16:57
add comment

I am not familiar with the tool itself, nor I am an expert on compiler optimization, but the reason could lie in the line:

Tile adjacent = _tiles[(int)myTarget.CurrentTile.X + x, (int)myTarget.CurrentTile.Y + y];

What could happen here is that the a Tile adjacent is created on the stack, with values copied from the the right tile in _tiles[] (you are not using what would be a reference in C++, and I don't know if they exist in C#). Therefore, you are continuously creating and destroying a Tile object, and moreover, inside 2 big nested for loop. Although it's on the stack, this could definitely affect speed. What maybe happened is that the Perf Analysis mode recognized this scenario, pre-allocated a Tile object outside the nested loop and kept on using it through the function, avoiding the overhead of creation/destruction.

share|improve this answer
    
I realized this problem early on and did fix it. It did not give me a noticeable performance boost nor did it change the magical speedup on perf analysis mode. –  ApoorvaJ Jul 14 '12 at 17:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.