-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBacktrackingPerformanceTests.cs
More file actions
69 lines (54 loc) · 2.01 KB
/
BacktrackingPerformanceTests.cs
File metadata and controls
69 lines (54 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Diagnostics;
using System.Linq;
using RegexParser.Matchers;
using Utility.BaseTypes;
using Msoft = System.Text.RegularExpressions;
namespace RegexParser.Tests.Performance
{
public static class BacktrackingPerformanceTests
{
public static void BacktrackingTest()
{
const int n = 20;
string input = new string('a', n);
string pattern = Enumerable.Repeat("a?", n).JoinStrings() + input;
Console.WriteLine("Input: {0}", input.Show());
Console.WriteLine("Pattern: {0}", pattern.ShowVerbatim());
displayTest("", () => countMatches(input, pattern));
// 7: 0.245 sec.
// 10: 0.273 sec.
// 15: 0.381 sec.
// 19: 1.921 sec.
// 20: 3.694 sec.
// 21: 7.385 sec.
displayTest("Microsoft:", () => countMatches_Msoft(input, pattern));
// 7: 0.002 sec.
// 10: 0.002 sec.
// 15: 0.005 sec.
// 19: 0.052 sec.
// 20: 0.103 sec.
// 21: 0.206 sec.
// 25: 3.315 sec.
// 29: 52.146 sec.
}
private static void displayTest(string title, Func<int> getMatchCount)
{
Console.WriteLine();
if (!string.IsNullOrEmpty(title))
Console.WriteLine(title);
Stopwatch stopwatch = Stopwatch.StartNew();
Console.WriteLine("Matches: {0:#,##0}", getMatchCount());
decimal elapsed = ((decimal)stopwatch.ElapsedMilliseconds) / 1000;
Console.WriteLine("Time: {0:#0.000} sec.", elapsed);
}
private static int countMatches(string input, string pattern)
{
return new Regex2(pattern, AlgorithmType.Backtracking).Matches(input).Count;
}
private static int countMatches_Msoft(string input, string pattern)
{
return new Msoft.Regex(pattern).Matches(input).Count;
}
}
}