Why does \\d in C# work slower than [0-9], and how to fix it?
Hello colleagues! I'm writing a log parser in C# using regular expressions. I noticed strange behavior: when I use \\d to search for digits, performance drops several times compared to [0-9].\n\nHere's an example of the code I'm testing:\n\n```csharp\nusing System;\nusing System.Diagnostics;\nusing System.Text.RegularExpressions;\n\nclass Program\n{\n static void Main()\n {\n string input = string.Concat(Enumerable.Repeat("1234567890", 100000));\n \n var sw = Stopwatch.StartNew();\n for (int i = 0; i < 1000; i++)\n {\n Regex.Match(input, @"\\d+");\n }\n sw.Stop();\n Console.WriteLine($"\\\\d: {sw.ElapsedMilliseconds} ms");\n \n sw.Restart();\n for (int i = 0; i < 1000; i++)\n
No answers yet.
Log in to leave an answer