Game Development Reference
In-Depth Information
Optimizing Game Performance
To make games that are fast and don't require a powerhouse to run, we must learn how to
find and fix bottlenecks. Good news is that if you wasn't thinking about performance to
begin with, your program can usually be optimized to run twice as fast just by eliminating
one or two biggest bottlenecks.
We will be using a copy of the prototype code to keep both optimized and original version,
therefore if you are exploring sample code, look at 04-prototype-optimized .
Profiling Ruby Code To Find Bottlenecks
We
will
try
to
find
bottlenecks
in
our
Tanks
prototype
game
by
profiling
it
with
ruby-prof .
It's a ruby gem, just install it like this:
$ gem install ruby-prof
There are several ways you can use ruby-prof , so we will begin with the easiest one.
Instead of running the game with ruby , we will run it with ruby-prof :
$ ruby-prof 03-prototype/main.rb
The game will run, but everything will be ten times slower as usual, because every call to
every function is being recorded, and after you exit the program, profiling output will be
dumped directly to your console.
Downside of this approach is that we are going to profile everything there is, including
the super-slow map generation that uses Perlin Noise. We don't want to optimize that, so
in order to find bottlenecks in our play state rather than map generation, we have to keep
playing at dreadful 2 FPS for at least 30 seconds.
This was the output of first “naive” profiling session:
Search WWH ::




Custom Search