public void PerformanceTest() { const int n1 = 100, n2 = 500, n3 = 1800, n4 = 1850, n5 = 3500; Counter counter = new Counter(); MatchCollection2 coll = Factory.CreateMatchCollection(getMatches(0, counter)); IEnumerable <Match2> seq1 = coll.Take(n1), seq2 = coll.Take(n2); Assert.AreEqual(0, counter.Value, "Start."); Assert.AreEqual(n1 - 1, seq1.Last().Index, "First/n1"); Assert.AreEqual(n1, counter.Value, "First/counter/1"); Assert.AreEqual(n2 - 1, seq2.Last().Index, "First/n2"); Assert.AreEqual(n2, counter.Value, "First/counter/2"); Assert.AreEqual(n1 - 1, seq1.Last().Index, "Second/n1"); Assert.AreEqual(n2, counter.Value, "Second/counter/1"); Assert.AreEqual(n2 - 1, seq2.Last().Index, "Second/n2"); Assert.AreEqual(n2, counter.Value, "Second/counter/2"); IEnumerable <Match2> seq3 = coll.Take(n3), seq4 = coll.Take(n4), seq5 = coll.Take(n5); Assert.AreEqual(n3 - 1, seq3.Last().Index, "First/n3"); Assert.AreEqual(n3, counter.Value, "First/counter/3"); Assert.AreEqual(n4 - 1, seq4.Last().Index, "First/n4"); Assert.AreEqual(n4, counter.Value, "First/counter/4"); Assert.AreEqual(n5 - 1, seq5.Last().Index, "First/n5"); Assert.AreEqual(n5, counter.Value, "First/counter/5"); }
public static void NextMatchTest() { const int n1 = 5000, n2 = 5300, n3 = 5500, n4 = 7000; Counter counter = new Counter(); MatchCollection2 coll = Factory.CreateMatchCollection(getMatches(n1, counter)); Assert.AreEqual(0, counter.Value); for (Match2 match = coll.First(); match.Index < n2; match = match.NextMatch()) { ; } Assert.AreEqual(n2 - n1 + 1, counter.Value); for (Match2 match = coll.First(); match.Index < n3; match = match.NextMatch()) { ; } Assert.AreEqual(n3 - n1 + 1, counter.Value); for (Match2 match = coll.First(); match.Index < n4; match = match.NextMatch()) { ; } Assert.AreEqual(n4 - n1 + 1, counter.Value); for (Match2 match = coll.First(); match.Index < n2; match = match.NextMatch()) { ; } Assert.AreEqual(n4 - n1 + 1, counter.Value); }
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); }
public static void MatchCollections() { MemoryProfiler memoryProfiler = MemoryProfiler.StartNew(); int itemCount = 10000000; MatchCollection2 matches1 = Factory.CreateMatchCollection(Enumerable.Range(1, itemCount) .Select(i => Factory.CreateMatch(i, i, "x"))); memoryProfiler.CollectGC(); Console.WriteLine("Matches: {0:#,##0}", matches1.Count); displayMemoryProfiler(memoryProfiler, itemCount); // 41 bytes/match (itemCount = 10,000,000) // 36.9 bytes/match (itemCount = 13,000,000) memoryProfiler.Reset(); Match2[] matches2 = Enumerable.Range(1, itemCount) .Select(i => Factory.CreateMatch(i, i, "x")) .ToArray(); memoryProfiler.CollectGC(); Console.WriteLine("Matches: {0:#,##0}", matches2.Length); displayMemoryProfiler(memoryProfiler, itemCount); // 36 bytes/match (itemCount = 10,000,000) // 36 bytes/match (itemCount = 13,000,000) memoryProfiler.Reset(); List <Match2> matches3 = Enumerable.Range(1, itemCount) .Select(i => Factory.CreateMatch(i, i, "x")) .ToList(); memoryProfiler.CollectGC(); Console.WriteLine("Matches: {0:#,##0}", matches3.Count); displayMemoryProfiler(memoryProfiler, itemCount); // 38.7 bytes/match (itemCount = 10,000,000) // 37.2 bytes/match (itemCount = 13,000,000) memoryProfiler.Reset(); List <int> ints1 = Enumerable.Range(1, itemCount) .ToList(); memoryProfiler.CollectGC(); Console.WriteLine("Ints: {0:#,##0}", ints1.Count); displayMemoryProfiler(memoryProfiler, itemCount); // 6.7 bytes/match (itemCount = 10,000,000) // 5.2 bytes/match (itemCount = 13,000,000) memoryProfiler.Reset(); int[] ints2 = Enumerable.Range(1, itemCount) .ToArray(); memoryProfiler.CollectGC(); Console.WriteLine("Ints: {0:#,##0}", ints2.Length); displayMemoryProfiler(memoryProfiler, itemCount); // 4 bytes/match (itemCount = 10,000,000) // 4 bytes/match (itemCount = 13,000,000) }
public void OneMatch() { MatchCollection2 coll = Factory.CreateMatchCollection(new[] { Factory.CreateMatch(0, 0, "") }); Assert.AreEqual(1, coll.Count, "Empty."); coll = Factory.CreateMatchCollection(new[] { Match2.Empty }); Assert.AreEqual(1, coll.Count, "Match2.Empty."); }
public void Empty() { MatchCollection2 coll = Factory.CreateMatchCollection(getMatches(0, -1)); Assert.AreEqual(0, coll.Count, "Empty/IEnumerable."); coll = Factory.CreateMatchCollection(new Match2[] { }); Assert.AreEqual(0, coll.Count, "Empty/Array."); }
public void ManyMatches() { MatchCollection2 coll = Factory.CreateMatchCollection(getMatches(1, 0)); Assert.AreEqual(0, coll.Count, "1 to 0."); coll = Factory.CreateMatchCollection(getMatches(0, 0)); Assert.AreEqual(1, coll.Count, "From 0 to 0."); coll = Factory.CreateMatchCollection(getMatches(5, 5)); Assert.AreEqual(1, coll.Count, "From 5 to 5."); coll = Factory.CreateMatchCollection(getMatches(1, 10)); Assert.AreEqual(10, coll.Count, "From 1 to 10."); int from = 1000, to = 4000; coll = Factory.CreateMatchCollection(getMatches(from, to)); Assert.AreEqual(to - from + 1, coll.Count, string.Format("From {0} to {1}.", from, to)); Assert.AreEqual(to, coll[to - from].Index, "Index"); Assert.AreEqual(to, coll[to - from].Length, "Length"); Assert.AreEqual("Match" + to.ToString(), coll[to - from].Value, "Value"); }