-
-
Save PrimaryFeather/6890236f44f87df70ab3 to your computer and use it in GitHub Desktop.
Small performance test class to compare different ways of calling an instance method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by redge on 28.10.15. | |
*/ | |
package tests | |
{ | |
public class InterfaceTest extends PerformanceTest | |
{ | |
private const NUM_RUNS:int = 500000; | |
private var _runner:Runner; | |
private var _iRunner:IRunner; | |
private var _oRunner:Object; | |
private var _run:Function; | |
override public function setUp():void | |
{ | |
_runner = new Runner(); | |
_iRunner = _runner; | |
_oRunner = _runner; | |
_run = _runner.run; | |
} | |
public function testWithClass(multiplier:Number):void | |
{ | |
var numRuns:int = NUM_RUNS * multiplier; | |
for (var i:int=0; i<numRuns; ++i) | |
_runner.run(100); | |
} | |
public function testWithInterface(multiplier:Number):void | |
{ | |
var numRuns:int = NUM_RUNS * multiplier; | |
for (var i:int=0; i<numRuns; ++i) | |
_iRunner.run(100); | |
} | |
public function testWithObject(multiplier:Number):void | |
{ | |
var numRuns:int = NUM_RUNS * multiplier; | |
for (var i:int=0; i<numRuns; ++i) | |
_oRunner.run(100); | |
} | |
public function testWithFunction(multiplier:Number):void | |
{ | |
var numRuns:int = NUM_RUNS * multiplier; | |
for (var i:int=0; i<numRuns; ++i) | |
_run(100); | |
} | |
} | |
} | |
interface IRunner | |
{ | |
function run(count:int):void; | |
} | |
class Runner implements IRunner | |
{ | |
private var _miles:uint = 0; | |
public final function run(count:int):void | |
{ | |
for (var i:int=0; i<count; ++i) | |
++_miles; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment