Profiling an XNA game on Xbox

jwatte's picture

This code file implements a simple profiler for XNA games that run on the Xbox.
It allows you to measure the amount of time spent in different parts of your code,
and bins the different durations into statistics bins (so you can see if it's
"spiky" or even).

To use it, you simply include the class, and create static instances for the
parts you want to measure/report on.

  static Profile ThisSection = Profile.Get("This.Section");

Then, inside your code, you measure and "bill" time towards the instance:

  public void MyFunction()
  {
    using (IDisposable d = ThisSection.Measure())
    {
      // code to measure goes here
    }
  }

You can have as many such sections as you want. To print out the different
measurements, call Profile.Dump(), and it will be printed to the debug output.
You can also call DumpAndReset() to clear out the data after printing, and
start measurement from 0.

  Profile.Dump();

The overhead of this code is quite small. However, if you want to remove it, the
easiest way is to find the calls to Measure(), and comment out those lines using
an #if or single-line comment.

The code is released into the public domain.

AttachmentSize
Xna-Profiling.zip2.33 KB