Browse Source

[benchs] add some string API benchmarks

Simon Krajewski 7 years ago
parent
commit
c22d0722a0

+ 105 - 0
tests/benchs/src/cases/CharacterAccess.hx

@@ -0,0 +1,105 @@
+package cases;
+
+import hxbenchmark.Suite;
+using StringTools;
+
+class CharacterAccess extends TestCase {
+	function measureL1() {
+		var s = "a";
+		var suite = new Suite("length 1");
+		suite.add("charAt(0)", s.charAt(0));
+		suite.add("charCodeAt(0)", s.charCodeAt(0));
+		suite.add("fastCodeAt(0)", s.fastCodeAt(0));
+		return suite.run();
+	}
+
+	function measureL100() {
+		var s = "".lpad("a", 100);
+		var suite = new Suite("length 100");
+		suite.add("charAt(0)", s.charAt(0));
+		suite.add("charAt(50)", s.charAt(50));
+		suite.add("charAt(99)", s.charAt(99));
+		suite.add("charCodeAt(0)", s.charCodeAt(0));
+		suite.add("charCodeAt(50)", s.charCodeAt(50));
+		suite.add("charCodeAt(99)", s.charCodeAt(99));
+		suite.add("fastCodeAt(0)", s.fastCodeAt(0));
+		suite.add("fastCodeAt(50)", s.fastCodeAt(50));
+		suite.add("fastCodeAt(99)", s.fastCodeAt(99));
+		suite.add("charAt(erratic)", {
+			s.charAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		suite.add("charCodeAt(erratic)", {
+			s.charCodeAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		suite.add("fastCodeAt(erratic)", {
+			s.fastCodeAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		return suite.run();
+	}
+
+	function measureL10000() {
+		var s = "".lpad("a", 10000);
+		var suite = new Suite("length 10000");
+		suite.add("charAt(0)", s.charAt(0));
+		suite.add("charAt(5000)", s.charAt(5000));
+		suite.add("charAt(9999)", s.charAt(9999));
+		suite.add("charCodeAt(0)", s.charCodeAt(0));
+		suite.add("charCodeAt(5000)", s.charCodeAt(5000));
+		suite.add("charCodeAt(9999)", s.charCodeAt(9999));
+		suite.add("fastCodeAt(0)", s.fastCodeAt(0));
+		suite.add("fastCodeAt(5000)", s.fastCodeAt(5000));
+		suite.add("fastCodeAt(9999)", s.fastCodeAt(9999));
+		suite.add("charAt(erratic)", {
+			s.charAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		suite.add("charCodeAt(erratic)", {
+			s.charCodeAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		suite.add("fastCodeAt(erratic)", {
+			s.fastCodeAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		return suite.run();
+	}
+
+	function measureL1000000() {
+		var s = "".lpad("a", 1000000);
+		var suite = new Suite("length 1000000");
+		suite.add("charAt(0)", s.charAt(0));
+		suite.add("charAt(500000)", s.charAt(500000));
+		suite.add("charAt(999999)", s.charAt(999999));
+		suite.add("charCodeAt(0)", s.charCodeAt(0));
+		suite.add("charCodeAt(500000)", s.charCodeAt(500000));
+		suite.add("charCodeAt(999999)", s.charCodeAt(999999));
+		suite.add("fastCodeAt(0)", s.fastCodeAt(0));
+		suite.add("fastCodeAt(500000)", s.fastCodeAt(500000));
+		suite.add("fastCodeAt(999999)", s.fastCodeAt(999999));
+		suite.add("charAt(erratic)", {
+			s.charAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		suite.add("charCodeAt(erratic)", {
+			s.charCodeAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		suite.add("fastCodeAt(erratic)", {
+			s.fastCodeAt(index);
+			index += index;
+			index %= s.length;
+		}, var index = 0);
+		return suite.run();
+	}
+}

+ 21 - 0
tests/benchs/src/cases/StringScan.hx

@@ -0,0 +1,21 @@
+package cases;
+
+import hxbenchmark.Suite;
+
+using StringTools;
+
+class StringScan extends TestCase {
+	function measure20003() {
+		var s = "".lpad("a", 10000) + "bcd" + "".lpad("e", 10000);
+		var suite = new Suite("length 20003");
+		suite.add("indexOf find", s.indexOf("bcd"));
+		suite.add("indexOf nofind", s.indexOf("bce"));
+		suite.add("indexOf hit", s.indexOf("bcd", 10000));
+		suite.add("lastIndexOf find", s.lastIndexOf("bcd"));
+		suite.add("lastIndexOf nofind", s.lastIndexOf("bce"));
+		suite.add("lastIndexOf hit", s.lastIndexOf("bcd", 10003));
+		suite.add("split find", s.split("bcd"));
+		suite.add("split nofind", s.split("bce"));
+		return suite.run();
+	}
+}

+ 5 - 1
tests/benchs/src/hxbenchmark/Suite.hx

@@ -13,12 +13,16 @@ class Suite {
 		cases = [];
 	}
 
-	macro public function add(eThis:Expr, name:String, expr:Expr) {
+	macro public function add(eThis:Expr, name:String, expr:Expr, ?exprInit:Expr) {
+		if (exprInit == null) {
+			exprInit = macro {};
+		}
 		return macro @:privateAccess {
 			var f = function() {
 				var currentTime = haxe.Timer.stamp();
 				var targetTime = currentTime + $eThis.maxTimePerCase;
 				var numSamples = 0;
+				$exprInit;
 				do {
 					++numSamples;
 					$expr;