As I found out, AS3 compiler is too stupid to add even trivial optimizations.
I’ve been trying to optimize one of my heavy libraries I use everywhere for some time. It parses a lot of XML using e4x and creates many objects which it has to check using isSomething() function which is just 3 lines of code. So, this function is executed for every single object created.
Let’s check the test now.
var t: int;
var v: int = 5;
var w: int = 6;
for ( var i: int = 0; i < 5; i++ )
{
t = getTimer();
for ( var j: int = 0; j < 1000000; j++ )
{
var k: Boolean = func( v, w );
//var k: Boolean = v > w;
}
trace( getTimer() - t );
}
private function func( v: int, w: int ): Boolean
{
return v > w;
}
This executes on my Macbook Pro for 400ms.
If you uncomment the second line and comment the first one, execution time drops to 90-100ms. Which is 4 times less. But this is for 100000 calls, you can say. Remember that 30fps = 33ms for every frame, so one millisecond here and one millisecond there give noticeable result. And as I said the same short function is executed for every object. And there can be a lot of them. Though I don’t have to do it every frame but I don’t want my application freeze in the beginning either.
Moreover,
var t: int;
for ( var i: int = 0; i < 5; i++ )
{
t = getTimer();
for ( var j: int = 0; j < 1000000; j++ )
{
var k: int = value;
//var k: int = _value;
}
trace( getTimer() - t );
}
public function get value(): int
{
return _value;
}
it’s the same for getters and setters because they are also functions.
So I just replaced all calls of this function for actual code (6 occurrences). Every programmer who knows OOP will say that it’s bad to copy and paste code, if he doesn’t know why I did that. He might also go post on the forums that I am a noob, make a function of these pasted code snippets and will be proud of himself.
Here’s the question. Speed or OOP? While AS3 compiler is so dumb and can’t inline simple functions like this I’ll have to break readability in favor of execution speed.













Recent Comments