expensive function calls
by Wurzel on Jan.03, 2010, under flash performance
While optimizing an implementation of the shadow cast algorithm, I realised that function calls are really expensive in terms of performance. Look at this small example:
private var _iterations:int = 1000000; private function startTest():void { var inlineStartTime:int = getTimer(); for (var i:int = 0; i < _iterations; ++i) { 10 + 10; // just to use CPU time } trace("Inline : " + (getTimer() - inlineStartTime)); var externalStartTime:int = getTimer(); for (var j:int = 0; j < _iterations; ++j) { doSomething(); } trace("External: " + (getTimer() - externalStartTime)); } private function doSomething():void { 10 + 10; }
That comes to this result:
Inline: 94
External: 382
Thus, simple functions should be avoided in loops with many iterations if possible. Inline the statements instead.