Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

General Optimization

New Here ,
Apr 27, 2013 Apr 27, 2013

Hi everyone!

I have some questions about genral code optimization, i'll demonstreate using examples below:

1) is it better to use:      for(i  = 0 ; i < Array.length ; i++)

                                   {

                                       code...

                                   }

or:

                                         length = Array.length                   

                                        for(i  = 0 ; i < length  ; i++)

                                        {

                                            code...

                                        }

2) is it better to use : for (i = 0 ; i < NUM  ; i++)

                                  {

                                        if (tempArray.property == something)

                                             {

                                                  code...

                                             }

                                        }

or:                                  for (i = 0 ; i  <NUM  ; i++)

                                  {

                                        tempVar = tempArray

                                        if (tempVar.property == something)

                                             {

                                                  code...

                                             }

                                        }

3)(maybe somilar to 2..)  is it better to use: for (i = 0 ; i < NUM : i ++)

                                                                 {

                                                                 array = new object(....)

                                                                 }

or:                                                             for (i = 0 ; i < NUM : i ++)

                                                                 {

                                                                 tempObject =  new object(....)

                                                                 array = tempObject

                                                                 }

I have more questions like that, but answering those should get me started...

Also, is there aby fomral Adobe documentation regarding these things? I couldn'y find any...So if someone could refer me to such (if exists), I'd be thankful.

(Edit: by preformance I mean RAM usage and calculaton time - I'm developing for mobile so these are crucial)

Thank you!

TOPICS
ActionScript
842
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 27, 2013 Apr 27, 2013

this is from the optimization chapter in a book i wrote:

For-Loops , While-Loops and Do-Loops

The bottom line on fast-executing loops in Flash is reverse for-loops are the fastest.  If a stored list of same-type objects is needed in the loop, a reverse for-loop using a vector to reference the list of objects is fastest.

All three loops execute faster if you use an int for the iteration parameter than if you use a uint. All three loops execute faster if you decrement the loop variable, rather than i

...
Translate
Community Expert ,
Apr 27, 2013 Apr 27, 2013

this is from the optimization chapter in a book i wrote:

For-Loops , While-Loops and Do-Loops

The bottom line on fast-executing loops in Flash is reverse for-loops are the fastest.  If a stored list of same-type objects is needed in the loop, a reverse for-loop using a vector to reference the list of objects is fastest.

All three loops execute faster if you use an int for the iteration parameter than if you use a uint. All three loops execute faster if you decrement the loop variable, rather than increment it.  (Note: if you decrement a loop variable i and use i>=0 as the terminal condition, you will trigger an endless loop if i is a uint.)

  All three loops execute faster if you use a variable or constant for the terminal condition rather than an expression or object property. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 27, 2013 Apr 27, 2013

Thank you kglad.

So that covers 1 & 2. What about num 3?

Also , is there an Adobe document for those thing? I get a feeling I'm going to run into many more of these little quirks, and I guess it would be better if I could answer my own questions

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2013 Apr 27, 2013

the first is slightly faster when tested just now on my computer.  but it's not likely to be something that would be a factor under real-world situations. and, that may change when run in a different flash player version (though it makes some sense and there's reason to believe the results would be consistant across all flash players because there's an extra step in the 2nd code snippet).

also, there's a lot of mis-information on the internet about what's efficient and what's not efficient.  in addition, code that may be more efficient when run in one flash player version may not be more efficient when run in a different flash player version.

the only way to determine what is and, is not more efficient, is to test yourself:

var NUM:int = 10000000;

var array:Array = [];

var startTime = getTimer();

///* ~2000ms

for (var i:int = NUM-1; i >= 0; i--) {

    array = new Object();

}

trace(getTimer()-startTime);

//*/

/* 2050ms

var tempObject:Object;

for (var i:int = NUM-1; i >= 0; i--) {

    tempObject =  new Object();

    array = tempObject;

}

trace(getTimer()-startTime);

*/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 27, 2013 Apr 27, 2013

Awsome! Guess you're right, testing myself is the safest way

Thank you kglad!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 28, 2013 Apr 28, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines