While profiling the game I’m working on, I was surprised to discover that a very large chunk of my app’s performance cost comes from sound. After some tests, it seems that starting a single new sound playing takes more time than updating all the game’s enemies, projectiles, particles, animations, collision detection, etc. Here’s a simplified performance test:
public function playSound():void
{
var start:int = flash.utils.getTimer();
this.sound.play(); // this.sound is an embedded Sound.
var end:int = flash.utils.getTimer();
trace(end-start); // Averages about 9ms
}
and
public function doMath():void
{
var start:int = flash.utils.getTimer();
for(var i:int = 0;i < 15000;i++)
{
var x:Number = Math.cos(Math.random());
}
var end:int = flash.utils.getTimer();
trace(end-start); // Averages about 7ms
}
The first, “playSound” averages around 9ms per call over a few hundred calls. This is just starting the sound playing once.
The second, “doMath” is faster, despite the fact that it’s generating 15,000 random numbers and calculating their cosines.
My questions:
Are these numbers about right? And, if so, is there any way to improve the performance of sound.play()?