Skip to content

Instantly share code, notes, and snippets.

@VladimirCores
Last active January 16, 2018 16:35
Show Gist options
  • Save VladimirCores/e1b3333f21b5c2edf1cb4455240e05e6 to your computer and use it in GitHub Desktop.
Save VladimirCores/e1b3333f21b5c2edf1cb4455240e05e6 to your computer and use it in GitHub Desktop.
Haxe - Time zone parsing speed test
class Main
{
static public function main():Void
{
var it = 30;
var ra:Int = 0;
var rb:Int = 0;
var rc:Int = 0;
while(it-- > 0) {
var obj:TestResults = TimeUtil.RunTest();
ra += obj.a;
rb += obj.b;
rc += obj.c;
}
trace("> ==============================================");
trace("> RES : REGEX | CONST | SUBSTR = " + ra + " | " + rb + " | " + rc);
}
}
class TestResults {
public var a:Int;
public var b:Int;
public var c:Int;
public function new(a:Int, b:Int, c:Int) {
this.a = a;
this.b = b;
this.c = c;
}
}
class TimeUtil
{
private static var timeZoneExtractorConst:EReg = ~/\(([^)]+)\)/;
public static function RunTest():TestResults
{
var INTERATIONS = 1000;
var iterator = INTERATIONS;
var start = Date.now();
while(iterator-- > 0) {
TimeUtil.extractTimeZoneRegexp(start);
}
var resultRegEx:Int = cast (Date.now().getTime() - start.getTime());
start = Date.now();
iterator = INTERATIONS;
while(iterator-- > 0) {
TimeUtil.extractTimeZoneSubstr(start);
}
var resultRegExConst:Int = cast (Date.now().getTime() - start.getTime());
start = Date.now();
iterator = INTERATIONS;
while(iterator-- > 0) {
TimeUtil.extractTimeZoneSubstr(start);
}
var resultSubstr:Int = cast (Date.now().getTime() - start.getTime());
trace("> TEST: REGEX | CONST | SUBSTR = " + resultRegEx + " | " + resultRegExConst + " | " + resultSubstr);
return new TestResults(resultRegEx, resultRegExConst, resultSubstr);
}
public static function extractTimeZoneRegexp(date:Date):String
{
var extractedTimeZome:String = "";
var timeZoneExtractor:EReg = ~/\(([^)]+)\)/;
if (timeZoneExtractor.match(Std.string(date)))
extractedTimeZome = timeZoneExtractor.matched(1);
return extractedTimeZome;
}
public static function extractTimeZoneRegexpConst(date:Date):String
{
var extractedTimeZome:String = "";
if (timeZoneExtractorConst.match(Std.string(date)))
extractedTimeZome = timeZoneExtractorConst.matched(1);
return extractedTimeZome;
}
public static function extractTimeZoneSubstr(input:Date):String
{
var date:String = Std.string(input);
var bracketStartPosition:Int = date.indexOf("(");
var bracketEndPosition:Int = date.indexOf(")");
if(bracketStartPosition > -1 && bracketEndPosition > -1) {
return date.substring(bracketStartPosition + 1, bracketEndPosition);
}
return "";
}
}
@VladimirCores
Copy link
Author

Results - "substr" works 10-20 times faster than "regex" and almost the same speed as cached "regex"

TEST: REGEX | CONST | SUBSTR = 20 | 1 | 2
TEST: REGEX | CONST | SUBSTR = 20 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 16 | 1 | 11
TEST: REGEX | CONST | SUBSTR = 15 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 14 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 14 | 1 | 0
TEST: REGEX | CONST | SUBSTR = 14 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 15 | 0 | 1
TEST: REGEX | CONST | SUBSTR = 14 | 1 | 0
TEST: REGEX | CONST | SUBSTR = 13 | 1 | 0
TEST: REGEX | CONST | SUBSTR = 14 | 1 | 0
TEST: REGEX | CONST | SUBSTR = 14 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 15 | 2 | 0
TEST: REGEX | CONST | SUBSTR = 16 | 1 | 0
TEST: REGEX | CONST | SUBSTR = 15 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 17 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 18 | 1 | 1
TEST: REGEX | CONST | SUBSTR = 24 | 1 | 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment