Ahhh... right, sorry. So this only offers a performance increase if the state of a key has to be checked more than once in a frame.
Gotya.
Printable View
Ahhh... right, sorry. So this only offers a performance increase if the state of a key has to be checked more than once in a frame.
Gotya.
I'm not sure if this has already been explained in thread but if it has, I don't think I got it, plz try help.
If I have a bullet MC, I need to store the hitTest action in it for when it hits people MC's.
say the people MC's are named:
man1
man2
man3
etc...
man29
man30
Well instead of 30 If hitTest actions can't I simply have one which will be triggered if the bullet hits any of them?
Plz try explain this as best you can.
Thanks for your help.
ripX tells me that multi-dimensional arrays are slower than just the one dimension... But, is a 3D array slower than a 2D and faster than a 4D?
of course, because you have to look up the first dimension, then the second, then the third..
However I would say the speed loss is negligable..
Also here is a little speed boost I have got..
lets say we have a 2d array which is rather wide:
map = Array100x100();
for(var y = 0; y < 100 ;y++){
var dmap = map[y];
for(var x = 0; x < 100 ; x++){
trace(dmap[x]);
}
}
Bit faster. Would cut off the difference between dimensions IMHO.
cheers preddy.
Drunken post, which I really shouldn't do in this thread...
If you look at multi dim arrays in byte code it's frightening how much data has to be pulled up before actually getting the value you want.
Where poss. you should try and pre-calc the multi-dim values you want ( ie 99 times out of a 100 it's going to be map data ) and transfer it to a single dim array.
Squize.
PS. As ever, sexy code pred. Just once post something lame ;)
hello everyone,
i just lurked so far and read all of those nice tips, some of which I already knew, some new ones.
Now, a question:
does it make sense to incorporate all those tips in my current project with flash 2004 around the corner and with that possible performance increases ???
In my last projects I tried to go further down the road of OOP vs. my so far chaotic, but effective scripting.
Now a lot of performance issues are due this style of scripting.
For example the "dot" syntax is very cool to work with but a lot of times, the tell target is much faster.
But I think it's a pain in the butt to use so many different approaches to coding all the time....
Whaddaya think, is MM getting their act together or do I have to re-code every given project after i finished it, to make it perform better (very time consuming that...).
Also, maybe we should collect all known and working optimisations in a FAQ ?
With regard to multi-dimentional arrays being slow, I quickly wrote this which I was talking about to Random.
With a map editor this information can be produced easily and keep the array fast and 1 dimentional.Code:var mapL = 3;
var mapH = 3;
var l = 0;
map = [1, 2, 1, 2, 1, 2, 1, 2, 1];
for (var i = 0; i<mapL; i++) {
for (var j = 0; j<mapH; j++) {
t = attachMovie("tile", "tile"+i+"_"+j, d++);
t._x = i*32;
t._y = j*32;
t.gotoAndStop(map[l]);
l++;
}
}
RipX
Yes, 1 dimensional arrays are generally a bit faster. Since you have the map in one array, you can loop through it using only one loop:
code:
var mapL = 3;
map = [1, 2, 1, 2, 1, 2, 1, 2, 1];
for (var i = 0; i<map.length; i++) {
t = attachMovie("tile", "tile"+i, i);
t._x = (i % mapL) * 32;
t._y = int(i/mapL) * 32;
t.gotoAndStop(map[i]);
}
The advantage is that you can remove the variables l and d and use i instead. And you remove one nested for loop, which is good. You have to do some more calculations to place the tile, but the overall code runs a bit faster than the code RipX posted above.
I'd actually thought of that way but I didn't want to complicate things too much since I wanted to show just the one optimation! But nice one Strille for posting that :D
RipX
fantasio good question.
I'm still of the mindset that if a game works, then don't kill yourself trying to optimise it.
Use tellTarget and other straight forward optimisations where poss. but I've not sat in front of a routine for a while and tried to skim a few ms off the execution time.
I find having a think about the best way to actually write a routine before hand is a million times easier than writing something and then trying to optimise it. May be a really blatant statement, but it's something I've never really worried about too much before 'cause I always had the time to optimise things, but things have changed recently ( I've got into the habit of using quite long var names, which is a speed no-no, but it saves me so much time when it comes to debugging that I can cope with losing the odd ms ).
If I do actually optimise something then it'll be the always called inner loops.
As to the faq, mBenney is working on one atm ( I've stupidly said I'd do the OOP section ).
Going back to the 1 dim arrays for maps, how do you guys plan to include the attributes ? Skip every other byte or have a seperate attribute array ?
To be honest I think it'll be easier to just pre-calculate the current map data you need and store that in a temp. 1 dim array ( Using it in effect like a stack ).
Squize.
Hey guys,
Just doing some testing...
A simple if statement, seeing if i is not 0, 3 times. This runs at about 5600ms in Flash's inbuilt player.Code:timer = getTimer();
var temp = 0;
for (var i = 0; i<100000; i++) {
if(i && i && i){
temp++;
}
}
timer = getTimer()-timer;
The EXACT same code, but with the if's expanded results in a cost of 4800ms.. Thats a fair bit!Code:timer = getTimer();
var temp = 0;
for (var i = 0; i<100000; i++) {
if(i){
if(i){
if(i){
temp++;
}
}
}
}
timer = getTimer()-timer;
My conclusion, Flash's compiler stinks so hard, I want to puke.
is that with the "Omit trace actions" set or clear in the publish settings?
Urg, kill me.. Didnt have it checked =|
Still it's important to have it checked. i, for one, never bothered with it but you can bet anything i make now will have it checked.
It's always quicker to split your If statements up ( And avoid and,or ) because Flash has to get all the data needed for ALL the checks before it can perform even the first one, so with an if statement with 3 conditionals to check, even if the first one fails to meet your condition it's still got another 2 lots of data first ( And if you throw some array[offset] checks into that as well it's a stupid amount of code generated ).
As a follow up question, I've seen a lot of people use the switch statement a lot recently, although there's not meant to be any performance improvement from it. Anyone know for sure ? Cheers :)
Squize.
Squize, I made a comparison between if-else and switch, and it appear that they are in fact equally fast (or slow, this is Flash we're talking about ;) )
This is a bit surprising, one would think that switch would be faster since you evaluate the value once and then jump to the right code, while if-else has to evaluate each if-statement until one is true or there are no more. Could it be that the switch-statement is compiled as a series of if-else statements?
Anyway, here's the code I used:
code:
a = 4;
time = getTimer();
for (var n=0;n<10000;n++) {
if (a == 1) {
b = a;
} else if (a == 2) {
b = a;
} else if (a == 3) {
b = a;
} else if (a == 4) {
b = a;
}
}
trace("if-else: "+(getTimer()-time));
time = getTimer();
for (var n=0;n<10000;n++) {
switch(a) {
case 1:
b = a;
break;
case 2:
b = a;
break;
case 3:
b = a;
break;
case 4:
b = a;
break;
}
}
trace("switch: "+(getTimer()-time));
In either case, I think switch looks better...
Thanks mate!
You're just a sexy actionscript writing machine atm :)
Squize.
Just thought i would drop a note to say that i have collected the most usefull tips from this thread in a page on my webspace as a list split into three sections: graphics/code/misc
Just thought i would make it easier to search through. (Link in sig)
random10122, that's a very good idea. Reading through this whole thread can probably be a bit dry...
Squize, I'm trying my best to be as sexy as humanly possible. :)
Btw Squize, I'm naming you the current champion of the use of the word "sexy" in your posts:
Squize: 16 (887 posts)
BlinkOk: 5 (2785 posts)
Strille: 2 (620 posts)
I_am_TheFlasher: 1 (400 posts)
nGFX: 1 (159 posts)
Saphua: 1 (381 posts)
Tonypa: 1 (2425 posts)
marmotte: 0 (1562 posts)
iopred: 0 (64 posts)
If someone can find a Games member with a higher count before this post was posted, let me know :)
:D
You know that people who talk about it the most aren't actually having it.
I only use that term so often cause the phrase "Donkey on woman action" is hard to slip into a sentence about actionscript.
Enough spam from me.
Squize.
Check "io::pred" and PrED32 Strille =)
Hehe this is the first time I look in this threath.. finding ways to improve my code for SaphuA... sum pretty handy stuff in here :D...Quote:
Originally posted by strille
Btw Squize, I'm naming you the current champion of the use of the word "sexy" in your posts:
Saphua: 1 (381 posts)
If someone can find a Games member with a higher count before this post was posted, let me know :)
Oh and damn... are you sure I only used sexy once? I mean, I'm very sexy, atleast sexy'er then a sexy person who think's he's very sexy...
And you know what? Yesterday a sexy girl walked by... and I walked (using my sexy walk) to her, saying:
"Hey! sexy... I was just thinking, woohoo you sure are sexy" and the sexy girl walked to me (using her sexy walk asswel) and then with a smooth sexy move... SHE STOLE MY sexy HOTDOG!!!
Now that sure was NOT sexy at all...
I think it's time to update your sexy list, sexy Strille ;)
Greetz,
sexy SaphuA
Sorry SaphuA, remember: "If someone can find a Games member with a higher count before this post was posted, let me know"
I think we have to take this to the Coffee Lounge soon :)
d0h!
My post was before your post... only FK was slow, and I could look in the future :rolleyes:
Hey,
I was just playing with things for my ISO game and checking everything what will be slightly faster...
Now I came across this code:
code:
//---------------------------------------
time = getTimer();
var y = 0;
while (y<2000/20*40) {
y++;
}
trace("Action 1: "+(getTimer()-time)+"ms");
//---------------------------------------
time = getTimer();
var y = 0;
var yy = 2000/20*40;
while (y<yy) {
y++;
}
trace("Action 2: "+(getTimer()-time)+"ms");
//---------------------------------------
time = getTimer();
var y = 0;
while (y<2000/20*40) {
y++;
}
trace("Action 1: "+(getTimer()-time)+"ms");
//---------------------------------------
time = getTimer();
var y = 0;
var yy = 2000/20*40;
while (y<yy) {
y++;
}
trace("Action 2: "+(getTimer()-time)+"ms");
//---------------------------------------
I don't get why action 2 is slower... it should be faser, right? Xplain plz :D
It's because 2000/20*40 is never calculated at run time, it's saved in the .swf as 4000. y<4000 is faster than y<yy because in the first you have one variable and one constant while you have two variables that need to be compared in the second.
Thnx :D,
So:
code:
var bleh = 16*12/9+6; //Is pre-calculated?
Then how about:
code:
var X = 16;
var Y = 12;
var bleh = X*Y/9+6; //Is this also pre-calculated?
The first example is pre-calculated but I don't think the second is.
I'd actually think it was since it is not exclusivly updating x/y at any time, it simply isn't hardcoded.
RipX
I don't know what this proves, but if you compile a new movie with the code:
code:
var X = 16;
var Y = 12;
var bleh = 16*12/10+6;
or the code where you have calculated the value instead:
code:
var X = 16;
var Y = 12;
var bleh = 25.2;
...you get the same file size (79 bytes in my case). But this will result in a file that's 101 bytes:
code:
var X = 16;
var Y = 12;
var bleh = X*Y/10+6;
Which makes me believe that the last example is not pre-calculated but rather calculated at run time depending on X, Y.
The file size is increased because the values are pulled out of variables which increases the bytecode size ( The first two snippits simply push values onto the stack, whereas the last one has to push the vars onto the stack and then get there values ).
As to pre-caluating things, it's a kinda moot point. Any value you use a lot in a loop you define outside it ( Or as much as possible ), and for any totally static value you define as a _global or in your (load) event.
Squize.
Besides all tricks within Flash, I optimize my SWFs with Flash Optimizer .That IS a good tool.
mannnia, are you working for SoftInfinity or something? All your posts up to this point have just been promotion of Flash Optimizer... :rolleyes:
:eek: how about that I found this tool exceptionally handy and just can't help talking about it! :D
btw, here's a good article at http://www.sitepoint.com/article/1108/ for faster Flash. These might be of no secret for those experienced in Flash, but I thought it could help, too.
Hope it's not making you think I work for Sitepoint, strille? :rolleyes:
regards,
...and I think you're getting filesize optimisation mixed up with code optimisation. As a simple rule of thumb, the faster the code the larger it is. Strange, but true :)
Another quick question, nothing major, I'm just curious about it. I've always been lead to believe that the maths functions in Flash are faster than using pre-calculated tables, but does anyone know if that's a 100% true ?
For anyone who doesn't understand what I'm taking about, would it be quicker to create an array full of sine values than to use Math.sin ?
No biggie, and I don't really mind not getting an answer, just curious.
Squize.
[edit]
Regards that link you just posted, point 2. Use very few -- if any -- raster graphics.. Kinda put me off reading anymore. I'd rather have a quicker running game than a quicker loading game I'm afraid.
[/edit]
mannnia, if you're not associated in any way with Flash Optimizer then I am truly sorry. I just think it's peculiar when a new user's first four posts on the forum are revolving around the excellence of a software. I guess it's just that good. Btw, welcome to the forum!
P.S. Do you work for Sitepoint? :D
strille, :DQuote:
Originally posted by strille
I just think it's peculiar when a new user's first four posts on the forum ...
well, I guess I am a peculiar new user... ;)
Thanks for welcoming to the forums! ;)
A few optimization hint to think about (this does NOT affect the compilers optimizations or the size of the file but rather the speed at which the same code in effect is processed at runtime where it really matters):
First, when using multiplication or division you can always improve the processing speed by using shift operators in their place.
operator clock ticks
--------------------------------------------------
multiplier 30+
divisor 90+
shift operators 3 to 6
Second, try unrolling for loops in processor intense areas. For example: a loop of 1 to 100 can be changed to 1 to 20 stepping by 5. Process the first iteration, then the second iteration+1, the third+2 etc....increment the loop by 5 and start over. This executes the same amount of code per iteration but causes that area of code to process only 20% of what it was doing prior.
Optimizing Collision Detection - Discussion and Examples
That thread is from a while back, just forgot to put it in the stickes when we were done with it. Manely goes over improving FPS by optimizing collision detection, with a focus on the centralized w/ bookeeping approach.