A Flash Developer Resource Site

Page 3 of 21 FirstFirst 123456713 ... LastLast
Results 41 to 60 of 410

Thread: Optimization Tips

  1. #41
    Junior Member
    Join Date
    Apr 2003
    Posts
    10
    I just tested to hide the "off-switched" lights in my pinball table by stopping at an empty frame. I did it with "tellTarget" as this is supposed to be 25 times faster than dot-syntax or "with" (flasm site).
    Anyway the whole game slowed down.
    In this case the power to switch states by "gotoAndStop" or "attach/remove" drag more performance than just keeping all lights on the stage and changing there _visibility.
    Seems like: if you have a lot of on/off switching to do fastly, you better use _visibility; otherwise i guess displaying an empty frame ist better, because it (hopefully) costs less drawing time. Perfect seems attach/remove, if you have the CPU Power to do so. Offstage-clips should be removed anyway.
    3 ways to get nothing. :-)

    Concerning scrolling, i think it is best to place off-screen-clips on screen again by just altering their coords instead of removing/attaching - if your clips allow to do so due to their content.

  2. #42
    w00t io::pred's Avatar
    Join Date
    Mar 2003
    Location
    Sydney, Australia
    Posts
    163
    Yeah, we proved that changing the frames of objects and repositioning then around the screen was the fastest method a while ago:

    http://members.optushome.com.au/chri...scrollGoTo.swf

    is the result...

  3. #43
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    IO Pred: Great game!

  4. #44
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Cheeky little game pred

    The threads( If anyone is interested ) about scrolling methods are http://www.flashkit.com/board/showth...l&pagenumber=1 and http://www.flashkit.com/board/showth...light=scroller

    Dunno if it's still good reading or not

    Squize.

  5. #45
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    Squize: How do you use Diagonals for platforms?

  6. #46
    Junior Member
    Join Date
    Apr 2003
    Posts
    10
    pred/squize: just hit the flashkit (looking for advanced AS Coding) and... it takes me a bit to get an overview... :-) Interesting scrolling thread though! Seems like i found my place.

  7. #47
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    adityadennis:
    As ever http://outsideofsociety.idz.net/ has a good tut on it, failing that pred will be a better person to ask than me ( I like shooters not platformers I'm afraid ).
    You'd be suprised, for the low number of posts he's not bad

    leif0:
    Yeah, when the boards good it's excellent but it seems to have highs and lows ( Mainly when people really don't wanna use the search function or when only one game type is in vogue ).

    Squize.

  8. #48
    w00t io::pred's Avatar
    Join Date
    Mar 2003
    Location
    Sydney, Australia
    Posts
    163
    stfu squize you bistach =)

    26 posts plus the 500 on my old one, damned moderators wont combine the accounts =(

  9. #49
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Sorry I seem to keep bringing this thread back from the dead but it's a good one and it's just handy having a load of tips in one place.

    Check out www.zoode.org there's a fantastic description of loop un-rolling ( Something which I totally forgot about ) and a port of Duff's Device.

    Basically no matter how quick your conditional loop code ( while (--x) ) you've still got to run that conditional check x number of times. Say if x=11 (ie we are running the loop 10 times) and we are just calling a function 10 times within that loop just set x=(10/2)+1 and then call the function twice within the loop. This halves the number of conditional checks ( And so on ).

    ( Taking this to the extreme you would remove loops all together but the file size would rocket, and it would just be impossible to amend the code. As strange as it sounds, faster code is usually longer ! )

    Duff's device is just a ( Very ) clever way of doing this which will work with any number of iterations. If you don't know the value for x ( Or if it may be a variable value ) then use this.

    There are some other good tips on the site ( Including a lot of excellent xml stuff ) so have a good look 'round.

    Squize.

    pred you know I love ya

  10. #50
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    Squize: Can you explain that again, I didnt really get it. How does "x= (10/2) + 1" optimize my code?

  11. #51
    w00t io::pred's Avatar
    Join Date
    Mar 2003
    Location
    Sydney, Australia
    Posts
    163
    if(var i = 0; i < 10; i ++){
    //this gets called 10 times
    }

    if(var i = 0; i < 10;i+=2){
    //this gets called 5 times
    //this gets called 5 times
    }

    is basically what its getting to..you can make a loop more efficient by unrolling it

  12. #52
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    On playing around I found that declairing a varible is much faster than not if it's in a for loop, around 17%-19%, for example:
    Code:
    MAX_VALUE = 39999
    function loop() {
    	var z;
    	for (i=0; i<MAX_VALUE; i++) {
    		z += 1;
    	}
    }
    loop();
    Is around 17%-19% faster on my machine than:
    Code:
    MAX_VALUE = 39999
    function loop() {
    	for (i=0; i<MAX_VALUE; i++) {
    		z += 1;
    	}
    }
    loop();
    I would imagine that in the first example it is storing the variable in memory and incrimenting it whilst in the second exampl it is not stroring it in the memory and having to recall it each time it loops (not 100% sure on that though)?? Squize/Pred can probably explain better than myself!

    RipX

  13. #53
    w00t io::pred's Avatar
    Join Date
    Mar 2003
    Location
    Sydney, Australia
    Posts
    163
    Weird, there is no logical reason why the second example runs slower, its just better coding practise to declare your variables before you use them.... but I mean, sure, if it works, lets go =)

    Basically squize's example means running through a loop half/quarter/eighth as much times, and just repeating the code...

    It all relies on:

    w00t();
    w00t();
    w00t();

    being faster than:

    for(var i = 0; i < 3;i++){
    w00t();
    }

  14. #54
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Yes it's totally true and it's even more so when unrolling. Using the same initial figure and unrolling to 10:

    Code:
    MAX_VALUE = 39999;
    function loop() {
    	var z;
    	for (i=0; i<MAX_VALUE; i += 10) {
    		z += 1;
    		z += 1;
    		z += 1;
    		z += 1;
    		z += 1;
    		z += 1;
    		z += 1;
    		z += 1;
    		z += 1;
    		z += 1;
    	}
    }
    loop();
    It was 25-30% faster when decairing the variable!!!!

    Here are the result without declairing the var. Running 5 tests in a row the results were:
    2297ms
    2306ms
    2337ms
    2280ms
    2295ms

    Here are the results when declairing the variable var z:
    1658ms
    1596ms
    1588ms
    1582ms
    1581ms

    Obviously a significant and not fluke difference!!

    RipX

  15. #55
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Thanks pred, I should have posted an example.

    adityadennis:
    You don't save on the code run in the loop, you save on the actual loop conditional code.

    A good way to think of it ( And optimisation in general ) is to give everything a time value, ie the number of milliseconds it takes to run ( I know it's just wishful thinking but are there cycle timings for AS ? )

    So say every command and var takes 5ms:

    for(var i = 0; i < 10; i ++){
    //this gets called 10 times
    }

    The first line takes 50ms ( That's just counting every individual element on that line using our 5ms pretend timing ) and the function in the loop takes 5ms ( Or 5 billion, doesn't matter ).
    Cause it's a loop it'll get run 10 times, so thats 10*50 = 500ms just for the loop code.

    So for the optimised example:

    for(var i = 0; i < 10;i+=2){
    //this gets called 5 times
    //this gets called 5 times
    }

    It still takes 50ms, there's still the same number of elements on the line, and the functions aren't any quicker in themselves. The difference is the loop is only running 5 times, ie half as many iterations. So now our loop code that really doesn't do anything that productive only takes 5*50 = 250ms. We've just halved the time to run a piece of code, which isn't bad

    The Duff's Device is just a really nice structured way to do the same thing. Basically, as with most optimisations, if you can slip it into your code without killing yourself then do it.

    RipX:
    Yeah declared vars run quicker.

    From what I know ( and guess ) the var declaration reserves some memory in the system pool and tokenises (sp?) the var to help speed things up at run time. Because of this you should try and avoid using var within a loop, it'll actually be slower. But if declared outside the loop it's quicker for Flash to look up.

    Squize.
    Last edited by Squize; 04-15-2003 at 06:56 PM.

  16. #56
    w00t io::pred's Avatar
    Join Date
    Mar 2003
    Location
    Sydney, Australia
    Posts
    163
    hmm:

    defining z:
    512
    509
    520
    512
    513
    507
    506
    516

    not defining z:
    745
    740
    739
    771
    739
    734

    My 'expert' opinion (yeah right) says that because your using var z, inside the function, your declaring a variable who's scope is that function, i'm willing to say that its faster because the variable is made as a register, because of its short lifespan..

  17. #57
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Flash6 does actually use register r:0 under certain conditions ( I don't think 5 uses them at all ? ), mainly for loop init code such as for / while loop code, but from what I've seen it doesn't use it too effectively, and with only 4 registers to play with it would soon struggle handling local variables.

    Who wants to search through figleaf for the answer ?

    Squize.

  18. #58
    w00t io::pred's Avatar
    Join Date
    Mar 2003
    Location
    Sydney, Australia
    Posts
    163
    not me! ehhe..

    but if it is not made as a register internally (on the CPU) then it might be done as a flash internal register, so it bypasses flash's lookup code...

  19. #59
    Timetravelling Superhero AJ Infinity's Avatar
    Join Date
    Oct 2002
    Location
    The Realms of Infinity
    Posts
    203
    Hmm, I understand. But I use for loops mainly. More efficient than while loops...
    Code:
    i = 0;
    while(10/2+1){
        doSomething(); 
    //I'm sure this loop will generate a "Script is causing Flash Player to run slowly" dialog :)
        i++
    }
    VS
    Code:
    for(i=0; i<10/2+1; i+=2){
         doSomething();
    }
    Haven't performed any test's on the "Duff's Device" or whatever Squize spoke about.
    From here to infinity,
    A J I N F I N I T Y

  20. #60
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    in some cercumstances your right!!!

    some test results here:
    Code:
    while loop without declaired var:
    4577 ms
    4503 ms
    4537 ms
    4488 ms
    4524 ms
    //
    while loop with declaired var:
    3170 ms
    3183 ms
    3138 ms
    3139 ms
    3153 ms
    //
    for loop without declaired var:
    3070 ms
    3089 ms
    3107 ms
    3059 ms
    3082 ms
    //
    for loop with declaired var:
    2135 ms
    2521 ms
    2096 ms
    2136 ms
    2115 ms
    RipX

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center