Tag Archive for 'code speed-up'

Code speed-up tips!

Isn’t it just great how a few simple tricks can speed up your code? Here are some…
Al examples below are based on the fact that calling a method is way slower than just calculating the answer on the fly.
Here are some fast ways to replace common Math methods. In most cases this save half the time or more :!:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//slow:
z = Math.abs(x);
//faster:
z = (x<0) ? -x : x;
 
//slow:
z = Math.min(x,y);
//faster:
z = (x<y) ? x : y; 
 
//slow:
z = Math.max(x,y);
//faster:
z = (x>y) ? x : y; 
 
//slow:
z = Math.floor(x);
//faster:
z = (x >>> 0); 
 
//slow:
z = Math.ceil(x);
//faster:
z = (x >>> 0) + 1; //mind the brackets! 
 
//slow:
z = Math.round(x);
//faster:
z = int(x); 
 
//slow:
z = Math.pow(x,2);
//faster:
z = x*x; //this one only works faster if the power is a fixed value; for-loops in case of a variable power are most of the time slower than the Math.pow!

Of course most of the above method replacements make your code more sloppy, so I suggest you only use these replacements when the speed of the application really matters (for example in huge for-loops) or in libraries or components which rarely need adjustment.

:idea: If you have any good additions to this list please let me know…

Use “this”…

Because “this” is just great!

Unfortunately many people omit them in their code :cry: . Not sure why though, but my guess is that they are ignorant or just lazy (don’t be offended if you don’t use the this keyword for another reason, but please let me know why).
Even Adobe (and Macromedia before them) neglects the this keyword. None of their examples uses this and it’s nowhere to be found in their own code either!
Shame on them!

At the company where I work we didn’t use the keyword up until a year or two/three ago. Some of us found the keyword to be very handy and convinced the rest (not sure which group I belonged to at that time).

First of all, using the this instance variables supplies you the option to declare a local variable with the same name:

1
2
3
4
this.myVar = "my instance var"; 
var myVar:String = "my local var"; 
trace(this.myVar); //my instance var 
trace(myVar); //my local var

(Note that the above code only works in a class and not in the timeline!)

This can come in really handy when you need to store some temporarily data, during a for loop for example.

The second and most important reason to use this, is that it keeps your code clean and neat. It will help you to visually distinguish the local variables from the instance variables. This is vital if you work in a team on the same project or if other people need to work on your project later on (bug fixing, implementing new feature, etc.)
In all these cases it’s important to scan through someone’s code quickly and see at a glance where a certain variable comes from or is declared. If you omit the this keyword you would first have to scan the entire method to see if, maybe, the variable is declared local in this method. While if you do use the this keyword you know for sure it is declared in the Class definition and you know you can use it throughout the entire class (and in case of a public var, throughout the entire code).

So using the this keyword gives you more control over your code and it lets you (and your colleagues) programme in old or other peoples code much faster!

If you don’t use the this keyword at the moment, I hope this post convinced you to try out the this keyword for a while (even if it’s only for one single project) and I’m sure you never want to programme with out it, ever again.

If you already did use the this keyword, maybe this post wil help you to convince some ignorant fools in your vicinity :D .