nemoblog

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 Reply

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