Exemple #1
0
 private static void EfficiencyTest(ISubstringSearcher searcher)
 {
     Console.WriteLine(searcher.Name + ':');
     Console.WriteLine($"\t最好情况:{Counting(searcher, bestText, bestPattern).ToString()} \t毫秒");
     Console.WriteLine($"\t正常情况:{Counting(searcher, normalText, normalPattern).ToString()} \t毫秒");
     Console.WriteLine($"\t最差情况:{Counting(searcher, worstText, worstPattern).ToString()} \t毫秒\n");
 }
Exemple #2
0
        private static long Counting(ISubstringSearcher searcher, string text, string pattern)
        {
            long cost = 0L;

            for (int i = 0; i < TEST_TIMES; i++)
            {
                cost += Timer.Counting(false, () => searcher.Search(text, pattern));
            }

            return(cost);
        }
Exemple #3
0
        private static void CorrectnessTest(ISubstringSearcher searcher)
        {
            for (int i = 0; i < 9; i++)
            {
                int    result, n;
                string text = RandomString(15);
                string pattern;

                if (i < 3)
                {
                    pattern = text;
                    n       = 0;
                }
                else if (i < 6)
                {
                    pattern = text.Substring(8, 5);
                    n       = 8;
                }
                else
                {
                    pattern = "zzzzzzzz";
                    n       = -1;
                }

                result = searcher.Search(text, pattern);
                if (result != n)
                {
                    Console.WriteLine($"{searcher.Name} 实现错误,result = {result.ToString()}");
                    Console.WriteLine(text);
                    Console.WriteLine(pattern);
                    return;
                }
            }

            Console.WriteLine($"{searcher.Name} 实现正确");
        }
Exemple #4
0
        public static void Invoke()
        {
            //CorrectnessTest(new BruteForce());
            //CorrectnessTest(new FiniteStateAutomata());
            //CorrectnessTest(new BoyerMoore());
            //CorrectnessTest(new CSharpImplement());
            //CorrectnessTest(new KMP());
            //CorrectnessTest(new GeneralBoyerMoore());

            ISubstringSearcher[] searchers = new ISubstringSearcher[]
            {
                new BruteForce(),
                new DeterministicFiniteStateAutomata(),
                new KMP(),
                new BoyerMoore(),
                new GeneralBoyerMoore(),
                //new CSharpImplement(),
            };

            foreach (var item in searchers)
            {
                EfficiencyTest(item);
            }
        }