-
Optimization test: -= faster than +=
I made a performace test recently...
Code:
var a = 100;
for (var i:int=0;i<10000000;i++){
a+=1
}
trace(getTimer());
//traces 410
and then...
Code:
var a = 100;
for (var i:int=0;i<10000000;i++){
a-=-1
}
trace(getTimer());
//traces 280
Both are doing exactly the same thing, but in different ways. The -= method did it much faster.
Can it be true?
-
I tested this a while ago with similar results.
-
Lawl, I just did something like that except it was num++ and num += 1; "+=" was a little quicker. One sec and I'll test that thing too.... Awesome, I already have the program up from testing other things. (I've been testing a lot of things since I just got this speed test code.)...
...Meh, they were about the same when I tried it. Test A = "+=" and Test B = "-="...
Test A: 119
Test B: 116
Test A: 114
Test B: 116
Test A: 114
Test B: 115
Tested them three times in a row since it's so fast. However, when I switch the "+= 1" with "++" I got this...
Test A: 197
Test B: 127
Test A: 201
Test B: 120
Test A: 209
Test B: 181
Not always a huge difference, but still faster. I should change all my for loops to += 1 instead of ++...
...This post is probably going to sound so odd because I keep trying stuff before submitting it.:P Anyways, when I tried different ways to do the for loop they all were about the same for some reason.:? Maybe CS4 does things a bit differently.
-
- is faster than +
--i is faster than i--
and since you're on performance, "for" is about 3x to 4x slower than "while" (that's for JS in all major browsers, and AS2... haven't tested in AS3 yet)... the tests below are a few years old.
Code:
flash player 8 javascript (IE6)
for while dowhile for while dowhile
2563 1078 1093 672 156 171
3344 500 515 672 172 172
657 500 562 687 187 172
3609 469 531 688 172 172
797 515 547 687 172 187
687 500 547 672 188 156
3500 516 531 687 157 172
922 500 531 672 188 188
766 1000 484 687 171 172
3235 484 1093
859 453 515
averages
2093.9 651.5 694.9 612.4 156.3 156.2
-
I posted this on Kirupa also, and someone pointed out that if you say "var a:Number", the difference will disappear.
-
Senior Member
var a:int = 100;
for (var i:int=0; i<10000000; i++)
119
var a:uint = 100;
for (var i:uint=0; i<10000000; i++)
537
var a:Number = 100;
for (var i:Number=0; i<10000000; i++)
1243
var a = 100;
for (var i=0; i<10000000; i++)
2785
I repeated in all cases and got similar results. int is fastest, followed by uint, followed by Number followed by none.
- The right of the People to create Flash movies shall not be infringed. -
-
Just tested the for vs while thing. I think they fixed that for AS3. Here's my code, in case I'm doing something wrong...
Code:
import flash.utils.getTimer;
var time:Number = getTimer();
function TestA():void
{
var i:int = 0;
time = getTimer();
while (i<100000000)
{
i += 1;
}
trace("Test A: ", (getTimer()-time));
}
function TestB():void
{
time=getTimer();
for (var i:int=0; i<100000000; i += 1)
{
}
trace("Test B: ", (getTimer()-time));
}
TestA();
TestB();
Tried that four times in a row, the outputs were...
Test A: 1173
Test B: 1080
Test A: 1125
Test B: 1121
Test A: 1116
Test B: 1109
Test A: 1261
Test B: 1105
I keep getting a weird fluke every once in a while where the while loop takes longer then the for loop. But overall I'd say they're about the same.
Edit: Oh yea, I forgot that I was doing 100,000,000 instead of 10,000,000. When I went back down to 10,000,000 the difference was like between 1-5 each time I ran it. A few results...
Test A: 109
Test B: 106
Test A: 109
Test B: 109
Test A: 106
Test B: 108
Last edited by VacantPsalm; 01-01-2009 at 11:43 AM.
-
For and while loops get compiled to the same bytecode in AS3 - the place you lose performance is in the increment and the conditional...so
PHP Code:
// slow - has to check if i is less than 10000000 every time
while (i<100000000){
i += 1;
}
// faster - just has to check that i is != 0
i = 100000000
while(i){
i += -1;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|