nemoblog

Tag: function

static functions

by on Jan.03, 2010, under flash performance

Static function calls are slower than normal ones. On the other side, creating a new instance is a very expensive operation. But when you call a function several times in a loop, it pays off using it in a non static way.

For a small test, I created this simple class with a normal and a static function.

public class CallMe {
 
 public function CallMe() {}
 
 public function doSomething(number:int):int {
 return ++number;
 }
 
 public static function doSomethingStatic(number:int):int {
 return ++number;
 }
 
}

Tested with this simple test code…

private var _iterations:int = 1000000;

private function startTest():void {
 var test:int = 0;
 var normalStartTime:int = getTimer();
 var testObj:CallMe = new CallMe();
 for (var i:int = 0; i < _iterations; ++i) {
 test = testObj.doSomething(test);
 }
 trace("Normal call: " + (getTimer() - normalStartTime));
 
 test = 0;
 var staticStartTime:int = getTimer();
 for (var j:int = 0; j < _iterations; ++j) {
 test = CallMe.doSomethingStatic(test);
 }
 trace("Static call: " + (getTimer() - staticStartTime));
}

…comes to this result:

Normal call: 352
Static call    : 422

Leave a Comment :, , , , more...

expensive function calls

by 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.

Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...

    Archives

    All entries, chronologically...