-
var tVerts:Vector.<Number> = new Vector.<Number>();
-
var inc:int = 0;
-
var t:Number, i:Number, j:Number, k:Number;
-
-
// fast way
-
t = getTimer();
-
for (i= -2; i<2; i+=.03){
-
for (j = -2; j<2; j+=.03){
-
for (k = -2; k<2; k+=.03){
-
tVerts[inc] = i;
-
inc++;
-
tVerts[inc] = j;
-
inc++;
-
tVerts[inc]= k;
-
inc++;
-
}
-
}
-
}
-
trace(getTimer() - t);
-
-
tVerts = new Vector.<Number>();
-
-
// slow way
-
t = getTimer();
-
for (i= -2; i<2; i+=.03){
-
for (j = -2; j<2; j+=.03){
-
for (k = -2; k<2; k+=.03){
-
-
tVerts.push(i, j, k);
-
// tVerts.push(j);
-
// tVerts.push(k);
-
}
-
}
-
}
-
trace(getTimer() - t);
Today I needed to populate a Vector in the above manner... populating the vector using push() was quite slow in this case because (among other reasons) push() is a method call. I was curious how much faster populating the Vector without push would be... so I wrote this snippet - in this case it was significantly faster - took less than a third of the time. You can run this snippet to see what kind of results you get.
It's important to note that this nested for loop could be optimized further but I wanted to focus on the push() optimization alone.
Warning
The most HORRIBLE thing about testing the speed of ActionScript code is that you CANNOT rely on the debug player to test optimizations. Always use the release version of the flash player. The debug player will give unusual (usually slower) results...