Game Development Reference
In-Depth Information
The game still doesn't feel fast enough, FPS occasionally keeps dropping down to ~45, so
we will have to do profile our code in smarter way.
Advanced Profiling Techniques
We would get more accuracy when profiling only what we want to optimize. In our case it
is everything that happens in
PlayState
, except for
Map
generation. This time we will
Map
generation happens in
PlayState
initializer, so we will leverage
GameState#enter
and
GameState#leave
to start and stop profiling, since it
happens after state is initialized. Here is how we hook in:
require
'ruby-prof'
class
PlayState
<
GameState
# ...
def
enter
RubyProf
.
start
end
def
leave
result
=
RubyProf
.
stop
printer
=
RubyProf
::
FlatPrinter
.
new(result)
printer
.
print(
STDOUT
)
end
# ...
end
Then we run the game as usual:
$
ruby 04-prototype-optimized/main.rb
Now, after we press
N
to start new game,
Map
generation happens relatively fast, and then
profiling kicks in, FPS drops to 15. After moving around and shooting for a while we hit
Esc
to return to the menu, and at that point
PlayState#leave
spits profiling results
out to the console: