-
-
Save VladimirCores/e1b3333f21b5c2edf1cb4455240e05e6 to your computer and use it in GitHub Desktop.
Haxe - Time zone parsing speed test
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
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 ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results - "substr" works 10-20 times faster than "regex" and almost the same speed as cached "regex"