Main Page

From ActionScript Performance Wiki

Revision as of 11:15, 25 April 2010 by Nicolas (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

This Wiki is dedicated to testing the performance of Actionscript 3 code on the Adobe ActionScript Virtual Machine.


Tests

Page Template

Simply copy and paste this wiki markup to to create a new test page:

=Purpose=
=Code=
<source lang="actionscript3">
Test code here...
</source>
=Results=
==Compiler version XY, Flash Player version XY, Operating System XY==
=Conclusion=

Test Classes

It doesn't matter what time of testing framework is in use to measure the code performance. Grant Skinner has an AS3 Performance Harness available for download. This is my own simple helper class:

package 
{
	import flash.system.Capabilities;
	import flash.utils.getTimer;
 
	/**
	 * Iterates through functions and returns the processing times.
	 * 
	 * @author Nicolas Schudel, Elkana Aron
	 */
	public class PerformanceComparison 
	{
		private var _iterations:int;
		private var _functions:Array;
 
		/**
		 * Iterates over funcitons and returns the execution times.
		 * 
		 * @param iterations The amount of times to perform the test.
		 * @param functions	Any number of functions to perform a test.
		 * @return A String with the individual testing times.
		 */
		public function PerformanceComparison(iterations:int,...functions:Array) 
		{
			_iterations = iterations;
			_functions = functions;
		}
 
		public function start():String 
		{
			var start:int = 0, i:int = 0, n:int;
			var times:Array = [], percentages:Array = [];
			var time:Number;
			var f:Function;
			var s:String, p:String;
			var functionsLength:uint = _functions.length;
 
			for(n = 0;n < functionsLength;n++) 
			{
				start = getTimer();
				i = _iterations;
				f = (_functions[n] as Function);
				while(i--) f();
				time = new Number(getTimer() - start);
				times[n] = time;
			}
 
			// Check fastest time and set to 100%.
			var fastestTime:Number = times[0];
			for each (time in times) if (time < fastestTime) fastestTime = time;
 
			// Add up the percentages.
			for each (time in times) percentages.push(Math.round(time * 100 / fastestTime - 100) || 0);
 
			// Put together the results using wiki syntax.
			s = "==";
			s += "Compiler version: , "; 
			s += "Player version: " + Capabilities.version.split(",").join(".") + ", ";
			s += "Operating System: " + Capabilities.os;
			s += "==\n";
 
			for (n = 0;n < functionsLength;n++) 
			{
				s += ";Test " + (n + 1) + "\t" + ": " + times[n] + " ms";
 
				if (percentages[n]) 
				{
					p = percentages[n];
					p = percentages[n];
					s += ", " + (p.length > 1 ? p : "0" + p) + "% slower" + "\n";;
				} 
				else 
				{
					s += ", best" + "\n";
				}
			}
			return s;
		}
	}
}