ActionScript Programming Tips - Efficiency

One of the misconseptions of programming, is that even though if a piece of code get's something done, it isn't allways the right way. In this series we're taking a look at the efficiency of scripts. The more efficent your scripts are, the faster they are executed in the Flash player. In small projects this can only be a few millisecons faster, but in projects where a lot of calculations need to be calculated per frame, this can really give a boost to the frame rate. For example: a badly scripted 3D engine (a project where LOADS of calculations need to be made) can get a framerate of like 10fps. While a optimised 3D engine, based on the same mathematics can get a framrate of 80fps.

Minimising Calculations

The smaller the amount of calculations that need to be calculated, the faster the script. Sounds logical eh? This is fairly simple to implement. You can save a frequently used value in a variable so it doesn't have to be calculated each time. For example, take a look at both of these preloader scripts:

_root.onEnterFrame = function () {
bt = _root.getBytesTotal();
bl = _root.getBytesLoaded();
if (bt == bl) {
gotoAndPlay(2);
}
}
//And this one:
bt = _root.getBytesTotal();
_root.onEnterFrame = function () {
bl = _root.getBytesLoaded();
if (bt == bl) {
gotoAndPlay(2);
}
}

The second script is the most efficent because it saves the total bytes of the movie ONCE, and not each frame. This value never changes anyway, so why keep updating it? This may not look very useful, but apply this to big scripts, and it can really get the scripts running faster. You can also apply this same technique with loops.

Variable Scopes

Variables have a thing called the scope. The scope is where the variable can be accesed from. You have variables that only can be accesed from the function where they belong to, variables that can be accessed throughout the whole MovieClip and variables that can be accessed throughout the whole movie. Here are the ways of defining different variables with different scopes:

var var1 = "value"; //first type
var2 = "value"; //second type
_global.var3 = "value"; //third type

After defining the variables, you don't need to repeat the var or _global. You can just type the name.

Anyway, getting back to efficiency. Using the first type, is allways the fastest, as the variables get deleted when your done with it. They can't be accesed from outside that current level at that time, so if you want to keep it for use in functions/events (like onEnterFrame), you need to use the second type. You can, however, acces this variable from outside the movieclip if you put the path in front of the variable. The slowest is _global. You should also avoid using this, as it increases the chance of accidently using the same variable name (and that allways causes loads of havoc).

Functions & Classes

Using a lot of functions can make the performance decrease. Each time a function is executed, Flash saves all the variables etc, and then executes the function. When the function is done, it takes back all the variables and continues the original script. This can sometimes really take some time, specialy when your using the code in a loop. Sometimes, using function is better because it increases the readibilty and useability of the script. If you want to make scripts accesible for other users, using functions is a must.

It's basicly also the same story for classes. If you want it to be accessible for other users, use functions & classes.Tips

You can go on and on testing your scripts, trying to find the fastest way to do it. But you won't seen any difference in just one execution. So if you want to test a scripts efficiency, put it in a loop of like 10,000 times. Then trace getTimer() minus the timer at the beginning of the test. Here's a little script:

startTimer = getTimer();
testvar = 0;
for (var t=0; t!=10000; t++) {
testvar++;
}
trace(getTimer()-startTimer);

image 1

Good luck!


Close    To Top
  • Prev Article-Flash:
  • Next Article-Flash:
  • Now: Tutorial for Web and Software Design > Flash > Basic > Flash Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction