-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMatcherPerformanceTests.cs
More file actions
157 lines (109 loc) · 5.82 KB
/
MatcherPerformanceTests.cs
File metadata and controls
157 lines (109 loc) · 5.82 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Diagnostics;
using RegexParser.Matchers;
using RegexParser.Tests.Asserts;
using UnitTesting;
using Utility.BaseTypes;
using Utility.Tests.Performance;
using Msoft = System.Text.RegularExpressions;
namespace RegexParser.Tests.Performance
{
public static class MatcherPerformanceTests
{
public static void VeryLongMatches()
{
const int times = 1, inputLength = 1000000;
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string lowercaseChars = EnumerablePerformanceTests.RepeatChars(alphabet, inputLength)
.AsString();
//Console.WriteLine("(Short input)");
//testRegexMatches("x", @".", times);
//// 0.171 sec.
testRegexMatches(lowercaseChars, @"", times);
// 2.138 sec. (inputLength = 1,000,000)
// 37.968 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, @"k", times);
// 2.907 sec. (inputLength = 1,000,000)
// 30.292 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, @"\w", times);
// 8.315 sec. (inputLength = 1,000,000) (!?)
// 16.459 sec. (inputLength = 2,000,000)
// 25.295 sec. (inputLength = 3,000,000)
// 41.248 sec. (inputLength = 5,000,000)
// 83.437 sec. (inputLength = 10,000,000) (64 bytes/match)
testRegexMatches(lowercaseChars, @"\w{1000}", times);
// 0.669 sec. (inputLength = 1,000,000)
// 6.272 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, @"(\w{1000})+", times);
// 0.857 sec. (inputLength = 1,000,000)
// 9.044 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, @"(\w{10000})+", times);
// 0.898 sec. (inputLength = 1,000,000)
// 9.242 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, @"(\w{20000})+", times);
// 1.173 sec. (inputLength = 1,000,000)
// 12.828 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, @"(\w{100000})+", times);
// 1.288 sec. (inputLength = 1,000,000)
// 13.453 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, @"(\w{10001})+", times);
// 12.556 sec. (inputLength = 1,000,000) (!?)
// 18.828 sec. (inputLength = 10,000,000)
testRegexMatches(lowercaseChars, alphabet, times);
testRegexMatches(lowercaseChars, alphabet.Substring(0, alphabet.Length / 2), times);
testRegexMatches(lowercaseChars, string.Format("({0})+", alphabet), times);
testRegexMatches(lowercaseChars, @"\w{1000000}", times);
testRegexMatches(lowercaseChars, @"\w+", times);
testRegexMatches(lowercaseChars, @"\w+n", times);
testRegexMatches(lowercaseChars, @"\w+e", times);
testRegexMatches(lowercaseChars + "7", @"\w+7", times);
testRegexMatches(lowercaseChars + "7", @"\w+?7", times);
testRegexMatches(lowercaseChars, alphabet.ToUpper(), times);
testRegexMatches(lowercaseChars, alphabet.ToUpper(), RegexOptions.IgnoreCase, times);
testRegexMatches(lowercaseChars, alphabet.Substring(0, alphabet.Length / 2).ToUpper(), RegexOptions.IgnoreCase, times);
testRegexMatches(lowercaseChars, @"[a-m]+", times);
testRegexMatches(lowercaseChars, @"[A-M]+", RegexOptions.IgnoreCase, times);
testRegexMatches(lowercaseChars, @"(\w{1,100000})+", times);
testRegexMatches(lowercaseChars, @"(\w{1,1000000})+", times);
}
private const bool useMemoryProfiler = false;
private static void testRegexMatches(string input, string pattern, int times)
{
testRegexMatches(input, pattern, RegexOptions.None, times);
}
private static void testRegexMatches(string input, string pattern, RegexOptions options, int times)
{
Console.WriteLine("Pattern: {0}", pattern.ShowVerbatim());
if (options != RegexOptions.None)
Console.WriteLine("Options: [{0}]", options.ToString());
MemoryProfiler memoryProfiler;
if (useMemoryProfiler)
memoryProfiler = MemoryProfiler.StartNew();
Stopwatch stopwatch = Stopwatch.StartNew();
MatchCollection2 matches = null;
//Msoft.MatchCollection matches = null;
for (int i = 0; i < times; i++)
matches = new Regex2(pattern, AlgorithmType.Backtracking, options).Matches(input);
//matches = new Msoft.Regex(pattern, RegexAssert.ToMsoftRegexOptions(options)).Matches(input);
if (useMemoryProfiler)
memoryProfiler.Reset();
Console.WriteLine("Matches: {0:#,##0}", matches.Count);
//string v;
//foreach (var m in matches.Cast<Match2>())
////foreach (var m in matches.Cast<Msoft.Match>())
// v = m.Value;
decimal elapsed = ((decimal)stopwatch.ElapsedMilliseconds) / 1000;
if (useMemoryProfiler)
{
long deltaBefore = memoryProfiler.DeltaValue;
memoryProfiler.CollectGC();
long deltaAfter = memoryProfiler.DeltaValue;
if (matches.Count > 0)
Console.WriteLine("Last: {0:#,##0} chars", matches[matches.Count - 1].Value.Length);
Console.WriteLine("Memory: {0,10:#,##0} bytes", deltaBefore);
Console.WriteLine("AfterGC: {0,10:#,##0} bytes", deltaAfter);
}
Console.WriteLine("Time: {0:#0.000} sec.\n", elapsed);
}
}
}