If Hierarchy

From ActionScript Performance Wiki

Jump to: navigation, search

Contents

Purpose

It is best to write the least processor intensive operations first inside an if statement. For example:

if (shortToProcess() && longToProcess())

But is if faster to separate the two into different if statements?

Code

package  
{
	import flash.display.Sprite;
 
	public class Test extends Sprite 
	{
		public function Test()
		{
			trace(new PerformanceComparison(100000, test1, test2).start());
		}
 
		private function test1():void 
		{
			if (false) if (timeConsumingOperation());
		}
 
		private function test2():void 
		{
			if (false && timeConsumingOperation());
		}
 
		private function timeConsumingOperation():Boolean {
			var i:int = 10000000;
			var arr:Array = [];
			while(i--) arr.push(i);
			return true;
		}
	}
}

Results

Compiler version: 3.3.0.485, Player version: MAC 10.0.2.54, Operating System: Mac OS 10.5.8

Test 1
43 ms
Test 2
43 ms

Compiler version: 3.3.04852, Player version: WIN 10.0.45.2, Operating System: Windows XP

Test 1
28 ms, best
Test 2
30 ms, 07% slower

Conclusion

The hierarchy does not matter, assumably both result in the same or similar ActionScript Byte Code.

Personal tools