ActionScript performance #loops

Continuing with the ActionScript performance series, I want to tell you a tweak on for-loops. So let’s start with the examples right away.

The regular syntax

Usually everybody learned to write a for-loop like this:

private function regularLoop() : void
{
	for (var i:int = 0; i < dataList.length; i++) {}
}

Now let’s try switching post increment to pre increment:

private function regularLoop2() : void
{
	for (var i:int = 0; i < dataList.length; ++i) {}
}

Next thing we will try is to type i to a uint (unsigned integer) because the counter field will never be negative. At least not in this case.

private function regularLoop3() : void
{
	for (var i:uint = 0; i < dataList.length; i++) {}
}

Switch the increment operator, too.

private function regularLoop4() : void
{
	for (var i:uint = 0; i < dataList.length; ++i) {}
}

The tweaked syntax

The worst thing about the regular syntax is that one always calls the length property on the array, on every iteration. Thinking about that, the best way to avoid that is pre-storing the length of the list in a field. So let’s look at our first tweaked solution:

private function tweakedLoop() : void
{
	for (var i:int = 0; i < length; i++) {}
}

Same steps here, so let’s try out uint and pre increment:

private function tweakedLoop2() : void
{
	for (var i:int = 0; i < length; ++i) {}
}
private function tweakedLoop3() : void
{
	for (var i:uint = 0; i < length; i++) {}
}
private function tweakedLoop4() : void
{
	for (var i:uint = 0; i < length; ++i) {}
}

The Test

Ok I set up a class, using Grant Skinner’s Performance Harness, to test all those solutions. Here are my results on a MacPro 2×2.8GHz Quad Core, 6GB RAM and Flash Player Debug 10,1,51,66.
Note that these values are average ms values.

Iterations Rl1 Rl2 Rl3 Rl4
50.000 926 935.5 987 995.25
100.000 1895 1896 1969.25 1979.75
500.000 9473 9500.25 9889 9913.25
1.000.000 18765.25 18988.75 19716.75 19895.25
Iterations Tl1 Tl2 Tl3 Tl4
50.000 88 88.25 119.75 119.5
100.000 177.75 177.5 231.5 231.25
500.000 881.5 881.75 1134.25 1175
1.000.000 1771.75 1777.25 2293.5 2312

You can download the source code for these tests. (Do  not forget to link your project to the Performance Test Class)

Conclusion

As you can see in this results, all of the regular synatx solutions are really bad practice, as they are really performance intensive due to the recalculating of the list.length on every iteration. That is the biggest advantage of the tweaked loop solutions.

Surprisingly, int turned out to be faster than uint. And it doesn’t really matter if you use pre or post increment in your loops. I would recommand to stay with post increment, because it turned out to be a, really tiny, bit faster than pre increment.

Hence, the optimal solution to code a for loop is this one:
Note that ‘length’ is a variable which contains the precalculated length of your array or vector.

for (var i:int = 0; i < length; i++) {
    // loop body
}

Tags: , ,

Leave a Reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>