c# - Benchmarking .Net regex options - or what does RightToLeft do? -
i've created demo application test performance of regexes. third test uses option righttoleft.
it seems speeds process lot! why? do?
here test app:
using system; using system.collections.generic; using system.diagnostics; using system.linq; using system.text; using system.text.regularexpressions; using system.threading.tasks; namespace consoleapplication1 { class program { private const string isrequestfordirectorywithouttrailingslashregex = @"^(?#path:)(.*/)?(?#lastpart:)(?<!\.asmx|\.aspx/)([^./?#]+?)(?#querystring:)(\?.*?)(?#anchor:)?(#.*)?$"; private static string[] tests = new string[] { "http://localhost/manager/page.aspx", "http://localhost/manager/", "http://localhost/manager", "http://localhost/manager/?param=value", "http://localhost/manager/dir?param=value" }; static void main(string[] args) { test1(); test2(); test3(); test4(); console.writeline(); console.readline(); } public static void test1() { regex regex = new regex(isrequestfordirectorywithouttrailingslashregex); dowork("1", regex); } public static void test2() { regex regex = new regex(isrequestfordirectorywithouttrailingslashregex, regexoptions.compiled); dowork("2", regex); } public static void test3() { regex regex = new regex(isrequestfordirectorywithouttrailingslashregex, regexoptions.compiled | regexoptions.righttoleft); dowork("3", regex); } public static void test4() { regex regex = new regex(isrequestfordirectorywithouttrailingslashregex, regexoptions.compiled | regexoptions.righttoleft | regexoptions.ignorecase); dowork("4", regex); } static void dowork(string name, regex regex) { stopwatch sp = new stopwatch(); sp.start(); (int = 0; < 100000; i++) { foreach (string s in tests) { regex.ismatch(s); } } foreach (string s in tests) { console.writeline(":" + s + ":" + regex.ismatch(s).tostring()); } sp.stop(); console.writeline("test " + name + ": " + sp.elapsedticks); } } }
the regexoptions.righttoleft
can useful when trying match pattern expect find @ end of input string, because documentation say: search goes right left left right starting @ final character in input string, regex though still applied left right.
your regex seems looking trailing slash directory paths, seems situation fits description.
although expression looking trailing slash, presence of 2 anchors (^
, $
) makes reasoning wrong, because regex can match in 1 possible way no matter starts.
i going keep looking actual reason behind this, leave answer is.
on other hand, .*/
part of expression right after (?#path:)
part @ start of expression consumes entire input string goes recursively each time find last /
, when starting search further ahead there might not lot of backtracking.
Comments
Post a Comment