Skip to content

Instantly share code, notes, and snippets.

@AxGord
Created November 24, 2019 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AxGord/e7bde138e6593636f66a245c5bd3a8e7 to your computer and use it in GitHub Desktop.
Save AxGord/e7bde138e6593636f66a245c5bd3a8e7 to your computer and use it in GitHub Desktop.
import haxe.Constraints.Function;
/*
Result:
src/Main.hx:16: Function bench
src/Main.hx:76: 0.019 sec
src/Main.hx:19: Signal bench
src/Main.hx:76: 0.01 sec
*/
class Main {
private static var RUN_COUNT: UInt = 100000;
private var model: Model;
private var fns: Array<Function>;
private function new() {
// warm
var v: Float = 0.00001;
for (_ in 0...10000) v *= 2;
trace(v);
trace('Function bench');
startBench(fnBench);
// model.print();
trace('Signal bench');
startBench(signalBench);
// model.print();
}
private function signalBench(): Void {
model = new Model();
fns = [
model.action1,
model.action2,
model.action3,
model.action4,
checkDisableThirdHandler
];
for (_ in 0...RUN_COUNT) dispatch(3);
}
private function dispatch(value: Int): Void {
for (f in fns) f(value);
}
private function fnBench(): Void {
model = new Model();
for (_ in 0...RUN_COUNT) makeActions(3);
}
private function makeActions(value: Int): Void {
if (model.isAllowFirst()) model.action1();
action23(value);
model.action4(value);
}
private function action23(value: Int): Void {
if (model.isAllowSecond()) model.action2();
if (model.isAllowThird()) model.action3(value);
checkDisableThird();
}
private function checkDisableThird(): Void {
if (model.get() >= RUN_COUNT) {
model.denySecond();
model.denyThird();
}
}
private function checkDisableThirdHandler(): Void {
if (model.get() >= RUN_COUNT) {
fns = [
model.action1,
model.action4
];
}
}
private function startBench(fn: Void -> Void): Void {
var d = Sys.time();
fn();
trace( Std.int((Sys.time() - d) * 1000) / 1000 + ' sec' );
}
private static function main(): Void new Main();
}
class Model {
private var counter: Int = 0;
private var allowFirst: Bool = true;
private var allowSecond: Bool = true;
private var allowThird: Bool = true;
public function new() {}
public function add(n: Int): Void counter += n;
public function get(): Int return counter;
public function isAllowFirst(): Bool return allowFirst;
public function isAllowSecond(): Bool return allowSecond;
public function isAllowThird(): Bool return allowThird;
public function denyFirst(): Void allowFirst = false;
public function denySecond(): Void allowSecond = false;
public function denyThird(): Void allowThird = false;
public function action1(): Void {
counter++;
}
public function action2(): Void {
counter += getValue();
}
public function action3(value: Int): Void {
counter += value;
}
public function action4(a: Int): Void {
var f = function(b: Int) counter += a * b;
f(2);
}
private function getValue(): Int {
return counter % 10 == 0 ? 2: 0;
}
public function print(): Void {
trace(counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment