Yesterday I read some oldschool articles about heavy optimization techniques from the demo-scene. One thing that has been mentioned was the use of throw. Throw slows your code down since there is an overhead for every function.
I do not want to go into detail but you can imagine that the AVM2 has to know that a function can throw. The problem is that I can not look into something like the FLASM instructions (which was possible for AS2). But the tests tell me that throw is slow! But not that slow.
It is about ~70ms at 10^6 iterations.
[as] var t0: Number;
var t1: Number;
var stop: Boolean;
stop = ( t0 && t1 );
var f0: Function = function( v: int ): int
{
if ( stop )
return 0;
return v++;
}
var f1: Function = function( v: int ): int
{
if ( stop )
throw new Error(’The error is that throw is slow.’);
return v++;
}
var i: int;
var j: int;
for ( var k: int = 0; k < 3; k++ )
{
i = 0;
j = 0;
t0 = (new Date()).getTime();
for ( i = 0; i < 1000000; i++ )
j = f0( j );
t1 = (new Date()).getTime();
trace( ‘No throw: ‘ + ( t1 - t0 ) + ‘ms’ );
i = 0;
j = 0;
t0 = (new Date()).getTime();
for ( i = 0; i < 1000000; i++ )
j = f1( j );
t1 = (new Date()).getTime();
trace( ‘With throw: ‘ + ( t1 - t0 ) + ‘ms’ );
}[/as]
So what I get from this is that throw costs only a little bit of time. If you want to optimize your code hardcore you have to kick out all the throw statements. You would also not use all the possible OOP features.
I will continue using throw. Sometimes a throw is not important but in some cases it is very handy. The speed gain without throw is not that much.




0 Responses to “Throw is (a little bit) slow!”