0% found this document useful (0 votes)
27 views

Code Optimization

The document compares code snippets that are not optimized versus optimized versions. It shows how variable declarations, function definitions, loops, and arithmetic operations can be rewritten to be more efficient by using built-in numeric types, avoiding unnecessary variable names, and leveraging bitwise operations. The optimized code snippets are more concise and make better use of language features to improve performance compared to the unoptimized versions.

Uploaded by

metaisaak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Code Optimization

The document compares code snippets that are not optimized versus optimized versions. It shows how variable declarations, function definitions, loops, and arithmetic operations can be rewritten to be more efficient by using built-in numeric types, avoiding unnecessary variable names, and leveraging bitwise operations. The optimized code snippets are more concise and make better use of language features to improve performance compared to the unoptimized versions.

Uploaded by

metaisaak
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Not optimized

Optimized

i = 0;

var i:Number = 0;

uint

int

Math.PI;

3.141592653589793;

function f(x) {
var y:int = x+1; // ...
}

var y:int;
function f(x:Number):void {
y = x+1; // ...
}

var verylongvariablename;

var t;

a = Math.PI+Math.E;
b = Math.PI-Math.E;

var PI:Number = Math.PI;


var E:Number = Math.E;
a = PI+E);
b = PI-E;

a.x++;
a.y++;

with(a) {
x++;
y++;
}

for(var a=1; a<10; a++){}


for(var b=2; b<20; b++){}
for(var c=3; c<30; c++){}

var i:int;
for(i=1; i<10; i++){}
for(i=2; i<20; i++){}
for(i=3; i<30; i++){}

// tiles
for(var i:int=0;i<10;i++) {
for(var j:int=0;j<10;j++) {
}
}

var tilelist:Array=[];
for(var i:int=0;i<tilelist.length;i++) {
}

for(i=0; i<10; i++){


}

i = 0;
while(i<10){
i++;
}

X = 5/2;
X*2;

5*.5;
X+X;

var x:int = a*2;


var y:int = b*16;
var z:int = c/4;

var x:int = a << 1; //2^1 = 2


var y:int = b << 4; //2^4 = 16
var z:int = c >> 2; //2^2 = 4

Math.abs(X);

X<0 ? -X : X;

(X ^ (X >> 31)) - (X >> 31)

Math.floor(X);

int(X);

X>>0;

a = 25*Math.PI/180;
b = 50*Math.PI/180;
c = 75*Math.PI/180;

const rad:Number = Math.PI/180;


a = 25*rad;
b = 50*rad;
c = 75*rad;

var x:int = Math.ceil(a,b);

var x:int = (a<b) ? a : b;

// if n is a multiple of x
if (n % x == 0) {}

if (n & (x-1) == 0) {}

var element:int = matrix[i][j];

var row:Array = matrix[i];


var element:int = row[j];

for (var i=0;i<array.length;i++) {}

for (var i in array) {}

X += 1;

X++;

if (this) { do; } else { sleep; };

this ? do : sleep;

X*(X+1)

X*X+X

Math.pow(X,5)

X*X*X*X*X;

0.876;

.876;

var a:Array;
var b:Array;
var c:Array;
Function main(e:Event):void {
a = [];
b = [];
c = [];
}

var a:Array;
var b:Array;
var c:Array;
var empty:Array = [];
Function main(e:Event):void {
a = b = c = empty;
}

a = [[[1]]];
b = a[0][0][0]+a[0][0][0];

a = [[[1]]];
c = a[0][0][0]
b = c+c;

var f:Boolean = a<b ? true : false;


if (!f) {}

var f:Boolean = a<b ? false : true;


if (f) {}

var a:int = 0;
var b:Array = [1,1,1];
a += b[0]+b[1]+b[2];

var a:int = 0;
var b:int = 1;
var c:int = 1;
var d:int = 1;
a += b+c+d;

!a && !b

!(a || b)

// int
if(a>b-1) {}

if(a>=b) {}

var

const

... ? code : null;

if (...) { code }

var t:int = a;
a = b;
b = t;

a ^= b;
b ^= a;
a ^= b;

i = -~i;
i = ~-i;

i++
i--;

i = -i;

i = ~i + 1;
i = (i^-1)+1;

x = a % b;

x = a&(b-1);

(i % 2) == 0
// Parity: 1 2 3 4 5 6 7 8 9

(i & 1) == 0

i++;
(i*.5)<<0!==i*.5<<0 ? ... : ...;

i++;
i&1 ? ... : ...;

var s:String = ;
s += hello;

var s:String = ;
s = s+hello;

var a:Object = {X:5};


var c:int = o.X+o.X+o.X;

var a:Object = {X:5};


var b:int = a.X;
var c:int = b+b+b;

// testing optimization
for (var i:int=0;i<10000000;i++)
// code testing
}
trace(getTimer());

You might also like