Local vs. External Variable

From ActionScript Performance Wiki

Jump to: navigation, search

Contents

Purpose

See if there is a performance between accessing a local variable and a accessing an external variable of an object.

Code

package 
{
	import flash.display.Sprite;		
 
	/**
	 * @author Nicolas Schudel
	 */
	public class Test extends Sprite 
	{
		public var n : Number = 0;
		public var vo : ValueObject = new ValueObject();
 
		public var n1 : Number = 123456;
 
		public function Test() 
		{
			trace(new PerformanceComparison(10000000, internalVar, externalVar).start());
		}
 
		private function internalVar() : void 
		{
			n = n1;
		}
 
		private function externalVar() : void 
		{
			n = vo.n2;
		}
	}
}
 
internal class ValueObject 
{
	public var n2 : Number = 123456;
}

Results

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

Test 1
time: 4162
Test 2
time: 4255 2% slower

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

Test 1
3060 ms, 01% slower
Test 2
3036 ms, best

Conclusion

Accessing a variable outside the current class is only slightly slower if the object keeps its type.

Personal tools