Finding bottlenecks with Time Profiler
After watching a WWDC talk on using time profiler, and noticing low framerate in my game, I decided to try using it to find out what I could optimize. To my surprise, I had a bottleneck that the profiler found very quickly. The culprit was this computed variable:
It was called twice every frame in the update method:
The issue was that the property was recomputed every time, which entailed enumeration through all of the scene's children. So by replacing it with a stored property set once at didMoveToView, I was able to eliminate two enumerations of the scene's children each frame. I don't remember the exact effect on CPU usage, but I think that the % of CPU my update function was using dropped from 30% to 2.4% or so, and the game now runs much more smoothly.
var playerSprite: PlayerSprite? {
return self["PlayerSprite"][0] as? PlayerSprite
}
override func update(currentTime: NSTimeInterval) {
guard didInitialize else {return}
camera!.position = playerSprite!.position
for child in self.children {
(child as? OverworldSpriteNode)?.updateWithDeltaTime(1/60)
}
k = (k + 1) % 7_200
if playerSprite?.position.isInRect(bounds) == false {
playerSprite?.gameOver() //not counting this one because the condition evaluates to false in the majority of frames
}
}
The issue was that the property was recomputed every time, which entailed enumeration through all of the scene's children. So by replacing it with a stored property set once at didMoveToView, I was able to eliminate two enumerations of the scene's children each frame. I don't remember the exact effect on CPU usage, but I think that the % of CPU my update function was using dropped from 30% to 2.4% or so, and the game now runs much more smoothly.
Comments
Post a Comment