Be a Supporter!

AS(3): Code style speed tests

  • 3,824 Views
  • 48 Replies
New Topic Respond to this Topic
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
AS(3): Code style speed tests 2006-07-28 07:13:43 Reply

not a tutorial, but a useful resource to people writing heavy applications requiring the fastest execution time:

AS:Main

these will probably apply to AS2 in the most part aswell (in terms of which is faster)

first one:

int,uint,Number, a loop of 100 000 000, simply increment and assignment to another variable

int: 563ms
uint; 2504ms
Number; 1234ms

now, the thing which occured to me here, and i quickly changed in all my applications as of yet, is that uint is for some reason. extremely slow!

as expected int is the fastest for an iteration loop like this, but it should be expected that uint would be the same as int, however for some reason it is very slow, doing more tests, it seems this is also the case, in otherwords, DONT use uint unless you have to

now, another important thing to note when building heavy applications needing as fast an execution as possible, although using an int for an iteration loop is much faster than Number, if you are then using that int in a floating point calculation, it can become alot slower because it has to convert the int to a Number, then back to an int again, so think about this.

next one:

strict data typing VS non strict data typing, and also declaration vs non declaration

again, an iteration loop, this time just int and Number (uint is kind of obsolete now)
(non declared are obviously, not typed either)

int (strict): 562ms
int (non strict): 6385ms
int (not declared): undefined
Number (strict): 1128
Number (non strict): 6385ms
Number (not declared): undefined

as expected, non strict is the same for both, well, because its the same loop, but notice the VAST difference between strict data typing the iteration loop, and non strict data typing

half a second, and one second, compared to 6 and a half seconds!

for the declaration, lets put it this way, it crashed flash player when i didnt declare because it took too long!, so lets try a smaller iteration for non declared comparison, only 2 000 000 this time!

declared Number (strict): 24ms
declared int (strict): 11ms
declared variable (no type still) : 134ms
undeclared variable: 10266ms

i think this is enough to change anyones views on not bothering to declare the variables, look at the difference! 10 seconds, compared to 0.1 second, and when strict data typing to Number, its 0.024seconds, and to int, 0.011second!!!

OK, next, lets see what the difference is in a for loop, for declaring the variable outside for loop, or in the for loop in the following way

var a:int; for(a = 0; a<100000000; ++a) a = a;
for(var a:int = 0; a<100000000; ++a) a = a;

first: 562ms
second: 581ms

so we can see, its very slightly faster to declare the variable outside the for loop, but this brings up the next one, how about ++a in comparison to a++ ?

++a : 553ms
a++ : 573ms

so we see, that ++a is very slightly faster

now, lets have a look at some array loops, first of all:

is it faster to do new Array(); or []; when creating a new array? (using a loop of 2 000 000)

Array(); 3141ms
[]; 1065ms

we can see, that its alot faster to create a new array via [] than new Array();

now lets try push() in comparison to just setting an undefined item in an iteration loop:
(10 000 000 loop)
var b:Array = [];
for(var a:int = 0; a<10000000; ++a) { b.push(a); }
for(var a:int = 0; a<10000000; ++a) { b[a] = a; }

push: 2049ms
[]: 1562ms

we can see here, that using the [] syntax in this style of populating an array is faster than push, but what happens if we define the initial array as new Array(10000000);

[]: 1455ms

if we use new Array() to set the size of the array, before populating it with [], its faster again

now. how about declaring variables outside of a loop, to be used within, rather than inside the loop:

i.e.
var b:int for(var a:int = 0; a<10000000; ++a){ b = a<<1; }
for(var a:int = 0; a<100000000; ++a){ var b:int = a<<1; }


out: 555ms
in: 535ms

so it is slightly faster to declare the variable to be used inside the loop:

anymore speed tests you would like me to run? just ask


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS(3): Code style speed tests 2006-07-28 07:23:04 Reply

Very useful for maximising the speed for your stuff :P I still can't work out why uint is so slow, though...


BBS Signature
ViktorH
ViktorH
  • Member since: Jul. 26, 2006
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:23:30 Reply

Nice speed tests. It's good to know when coding bigger games.
Btw, how do you test them?

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:24:38 Reply

At 7/28/06 07:23 AM, GuyWithHisComp wrote: Nice speed tests. It's good to know when coding bigger games.
Btw, how do you test them?

using getTimer();, basicly

import flash.utils.*;

var t:int = getTimer();
//whatever i want to test
trace(getTimer()-t);

so it gets the amount of time it took to execute whatever is inbetween


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:26:27 Reply

At 7/28/06 07:24 AM, -dELta- wrote: using getTimer();, basicly

Ah, cool. Never thought of that.
How is this AS3 then?


BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:28:07 Reply

At 7/28/06 07:26 AM, ViktorHesselbom wrote: How is this AS3 then?

because im doing the speed tests in flash 9 alpha, using AS3, and im also using the 'int' datatype and 'uint' datatype which aernt part of AS2, and because of the new virtual machine, the speed will be different than in AS2 for alot of things (although like i said, the which one is fastest, should remain the same)


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:31:13 Reply

ok, this is a very strange one

comparing ++a, to a++, and to a+=1

a+=1, although its speed is more eratic, and isnt so static, is faster than both ++a and a++

running the tests 10 or so times, i get averages around

a++: 570
++a: 559
a+=1: 499


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:38:19 Reply

At 7/28/06 07:31 AM, -dELta- wrote: comparing ++a, to a++, and to a+=1

Would that be any difference with a+=2?


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS(3): Code style speed tests 2006-07-28 07:39:58 Reply

Just something to add:

if(), conditionals and binary for defining a variable:

var xe:Boolean = true;
var xd:Number = 0;
//If
if(xe){
xd = 1.5;
};
else{
xd = 0;
};
//Conditional
xd = (xe) ? 1.5 : 0;
//Binary
xd = xe * 1.5;

When true:

If - 99ms avg
Conditional - 420 ms avg
Binary - 109 ms avg

When false:

If - 108ms avg
Conditional - 426 ms avg
Binary - 109 ms avg

This is by no means a scientific study; I just thought I'd see if I could get a rough idea of which worked faster. Flash 9 Alpha.


BBS Signature
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:42:09 Reply

At 7/28/06 07:39 AM, _Paranoia_ wrote: Just something to add:

So conditional suck at speed and looks weird. :S


BBS Signature
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 07:49:04 Reply

At 7/28/06 07:38 AM, ViktorHesselbom wrote:
At 7/28/06 07:31 AM, -dELta- wrote: comparing ++a, to a++, and to a+=1
Would that be any difference with a+=2?

delta: +=2 and +=1 cant really have a proper test, but they are roughly the same speed.
And booleans (rough):
if(a==true): 1929ms
if( a): 787ms
if(a==false): 1914ms
if(!a): 784ms


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS(3): Code style speed tests 2006-07-28 07:50:02 Reply

At 7/28/06 07:42 AM, ViktorHesselbom wrote:
At 7/28/06 07:39 AM, _Paranoia_ wrote: Just something to add:
So conditional suck at speed and looks weird. :S

Apparently slow; but you're still taking very fast here. Unless you're going to be using it in a loop with tens of thousands of passes you'll have a lot more to worry about than conditionals, and that one line can make your code so much more readable.


BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 09:16:34 Reply

int: <<2 : 552ms
int: >>2 : 574ms
int: *4 : 2551ms
int: /4 : 2775ms
int: *0.25: 2010ms

Number: <<2 : 2901ms
Number: >>2 : 2942ms
Number: *4 : 1320ms
Number: /4 : 1452ms
Number: *0.25 : 1224ms

so we can see, ints are faster with binary arithmetic
and Numbers are faster with everything else

and obviously, multiplying by the inverse of a number, is faster than dividing by it :)


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS(3): Code style speed tests 2006-07-28 09:54:11 Reply

delta try sins cosines tangents and sqrts now

Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS(3): Code style speed tests 2006-07-28 10:04:00 Reply

At 7/28/06 09:54 AM, Glaiel_Gamer wrote: delta try sins cosines tangents and sqrts now

Aren't they kinda unrealistic to use integers for? :P


BBS Signature
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS(3): Code style speed tests 2006-07-28 10:06:15 Reply

At 7/28/06 10:04 AM, _Paranoia_ wrote:
At 7/28/06 09:54 AM, Glaiel_Gamer wrote: delta try sins cosines tangents and sqrts now
Aren't they kinda unrealistic to use integers for? :P

you can have an integer input though

Depredation
Depredation
  • Member since: Sep. 5, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS(3): Code style speed tests 2006-07-28 10:55:03 Reply

What about -= and -- . Are there speeds the same as ++ and +=, or is it different?


BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 11:19:47 Reply

yeh, like ++a, a++, a+=1

a-=1 is the fastest, followed by ++a which is very silghtly, often the same as a++


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-28 11:21:23 Reply

sorry,

a-=1 is the fastest, followed by --a, closely, and often on par with a--

a-=1 around about 305ms
a-- and --a around about 344ms


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Depredation
Depredation
  • Member since: Sep. 5, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to AS(3): Code style speed tests 2006-07-29 05:30:58 Reply

At 7/28/06 11:21 AM, -dELta- wrote: sorry,

a-=1 is the fastest, followed by --a, closely, and often on par with a--

a-=1 around about 305ms
a-- and --a around about 344ms

Thanks. I'll try to remeber to use a-=1 more often then :)


BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-31 03:21:43 Reply

its been established already that using an if...else is faster than a ?... : .... conditional, so i wont do that one, but heres a comparison for int and Number in a large loop for Math.abs(), and using an if...else instead. but also whether its faster to use the prefix '-' to negate a number, or to multiple by -1. It should be clear that the prefix should be faster... but since weve already seen that +=1 and -=1 are faster than the prefixes ++ and --, we cant be too sure

the loop for both is from -10000000 to 10000000, with another variable of the same type being set to the modulus of the iterated variable

Number (if else -) : 214ms
Number (if else *) : 208ms
Number (abs) : 3111ms

int (if else -) : 327ms
int (if else *) : 335ms
int (abs) : 3157ms

first of all. we see that in all tests, int was outperformed by number, but also that Math.abs is very slow in comparison to the other tests, and like i said before with ++ and --, we cant be too sure.

for Number, multiplying by -1 outperformed the negating prefix, but for int, multiplying was slower.

it also seems, that if i refer to using non negative numbers in the iteration, int outperforms Number, rather than the other way round.

however one last test, what happens if i make my own modulus function? Will it still be faster than Math.abs(), or will the function call cause it to slow down?

function modulus(a:int):int
{
if(a<0) return -a;
else return a;
}
function modulus(a:Number):Number
{
if(a<0) return a*-1;
else return a;
}

int (function) : 2041ms
int (if else '-') : 327ms
int (abs) : 3157ms

Number (function) : 2212ms
Number (if else '*') : 208ms
Number (abs) : 3111ms

although the function usage is still qquite a bit faster than the Math.abs() call, its alot slower, basicly 10x slower than finding the modulus on the spot, this is one of the major downsides of functions it seems in AS, that is, they are very slow to call in comparison to rewriting the code where its needed.

Im not recomending you to have massive functions rewrote on the spot, that would be FAR too messy, but i would recomend with simple short functions like this, to write them on the spot.


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
RyanPridgeon
RyanPridgeon
  • Member since: Dec. 7, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS(3): Code style speed tests 2006-07-31 04:39:04 Reply

You sure have been doing some research :P


I make flashes because I can.
PM me for anything flash or web related or visit my blog!!

BBS Signature
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS(3): Code style speed tests 2006-07-31 07:30:28 Reply

Do you think you'll ever be able to stop looking so smart, Mr Delta?


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS(3): Code style speed tests 2006-07-31 08:53:03 Reply

Here's another one - modulo assignments versus if statements for resetting counters:

Modulo -

j += 1;
j %= 2;

If (equality) -

j += 1;
if(j == 2){
j = 0;
};

If (comparison) -

j += 1;
if(j >= 2){
j = 0;
};

Modulo assignment - 1082
Number equality check - 87
Number comparison - 91

If statements seem to be overall vary fast, compared to other functions, which isn't too suprising. They still look untidy, though :P


BBS Signature
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS(3): Code style speed tests 2006-07-31 09:06:49 Reply

does AS3 support inline functions?

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-07-31 09:54:01 Reply

At 7/31/06 09:06 AM, Glaiel_Gamer wrote: does AS3 support inline functions?

no


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS(3): Code style speed tests 2006-09-08 11:45:59 Reply

Care to test
Math.sqrt(x);
vs
Math.pow(x, 0.5);
?


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS(3): Code style speed tests 2006-09-08 11:52:50 Reply

At 9/8/06 11:45 AM, ViktorHesselbom wrote: Care to test
Math.sqrt(x);
vs
Math.pow(x, 0.5);
?

Not bad :P

It's all yours, anyon who wants to try it.


BBS Signature
phyconinja
phyconinja
  • Member since: Sep. 18, 2004
  • Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to AS(3): Code style speed tests 2006-09-08 12:13:20 Reply

I could probobly test it myself if my comp worked..

can you test the diference between "and" and "&&"??
and "or" and "||"?

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS(3): Code style speed tests 2006-09-08 12:29:13 Reply

using loop of 10,000,000

sqrt, int: 1902ms
pow,int: 4255ms
sqrt, Number: 1885ms
pow,Number: 4165ms

i cant say that im suprised with this mind, surely a specific function for square root is faster than an arbitary power function.

the loop itself, is done using int and Number for Number, so its harder to compare between int and Number, but since int loops are ALOT faster, it can be seen that using sqrt and pow with Numbers must also be ALOT faster, because Number outperformed int.


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature