Achieving Good Performance on Different Hardware Types

This topic demonstrates how to write games for a variety of hardware configurations.

Different hardware platforms provide different levels of performance. Even on the same platform—on a PC for example—variations in processing power, graphics capabilities, and hard-drive performance can create a high variance in the performance of your application.

By writing game code that prepares for, and adapts to, slower and less capable hardware, you allow your games to reach a larger audience while providing a great experience for gamers on all the platforms you target.

Hardware Specific Degradation

Single vs. Multiple CPUs

If a computer has multiple CPUs, some work can be offloaded to the other CPUs. The following code sample shows how to determine the number of available CPUs in the computer. For the game that takes advantage of, but does not require, multiple CPUs, this code sample describes where to place code for single processor and multiprocessor computers.

// Scale appropriately for the number of processors on this computer

if (Environment.ProcessorCount == 1)
{
    // Perform tasks specific to single processor systems.
}
else
{
    // Perform tasks specific to multi-processor systems.
}

Performance-Specific Degradation

You can measure performance levels by the number of times the Update method is called each second. Once the update rate falls below a certain level (the default is 60 frames per second [fps]), GameTime.IsRunningSlowly is set to true.

This code sample shows where to place code that should be skipped if the game's performance falls below 60 fps.

// Perform the following only if the game is running 
// at 60 frames per second or faster

if (gameTime.IsRunningSlowly == false)
{
    // Perform time consuming update or draw here.
}

If 60 fps is not ideal for the game, you can change the default by altering TargetElapsedTime. To have IsRunningSlowly become true when the game slows to less than 30 fps, include this in the program's initialization code.

// Alert the program when the frame rate falls below 30 frames per second.

TargetElapsedTime = new TimeSpan(10000000L / 30L);

To test for a different frame rate, change the 30 of the 30L to the desired fps (for example, 45L for 45 fps, or 100L for 100 fps).

Bb975668.bp(en-us,XNAGameStudio.41).gifBest Practice
To provide the best experience for your players, find the frame rate that your game can sustain continuously, and set that as the default frame rate. Having a smooth, constant frame rate is better than having a high, but variable, frame rate.

See Also

Concepts

Threading (C# Programming Guide)
Using Threading (C# Programming Guide)

Reference

IsRunningSlowly
TargetElapsedTime
System.Environment.ProcessorCount Property
ElapsedGameTime