Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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)
        }