Example #1
0
        public void PerformanceComparer()
        {
            var perf = new Stopwatch();
            var opt  = Stopwatch.IsHighResolution;

            var         list      = Enumerable.Range(1, 2000000).ToList();
            IKarateChop chopper   = null;
            int         index     = -1;
            int         searching = 2;

            perf.Start();
            chopper = new RecursionChopper();
            index   = chopper.Chop(searching, list);
            perf.Stop();
            Console.WriteLine(chopper.GetType().Name + " result=" + index + ": " + perf.ElapsedMilliseconds);
            perf.Reset();

            perf.Start();
            chopper = new ProceduralChopper();
            index   = chopper.Chop(searching, list);
            perf.Stop();
            Console.WriteLine(chopper.GetType().Name + " result=" + index + ": " + perf.ElapsedMilliseconds);
            perf.Reset();

            perf.Start();
            chopper = new FunctionalChopper();
            index   = chopper.Chop(searching, list);
            perf.Stop();
            Console.WriteLine(chopper.GetType().Name + " result=" + index + ": " + perf.ElapsedMilliseconds);

            perf.Start();
            chopper = new FunctionalChopper2();
            index   = chopper.Chop(searching, list);
            perf.Stop();
            Console.WriteLine(chopper.GetType().Name + " result=" + index + ": " + perf.ElapsedMilliseconds);
            perf.Reset();

            perf.Start();
            chopper = new MapReduceChopper();
            index   = chopper.Chop(searching, list);
            perf.Stop();
            Console.WriteLine(chopper.GetType().Name + " result=" + index + ": " + perf.ElapsedMilliseconds);
            perf.Reset();

            perf.Start();
            chopper = new BinaryTreeChopper();
            index   = chopper.Chop(searching, list);
            perf.Stop();
            Console.WriteLine(chopper.GetType().Name + " result=" + index + ": " + perf.ElapsedMilliseconds);
        }
Example #2
0
        private static void TestChops(IKarateChop karate)
        {
            Assert.AreEqual(-1, karate.Chop(3, new int[] { }));
            Assert.AreEqual(-1, karate.Chop(3, new int[] { 1 }));
            Assert.AreEqual(0, karate.Chop(1, new int[] { 1 }));

            Assert.AreEqual(0, karate.Chop(1, new int[] { 1, 3, 5 }));
            Assert.AreEqual(1, karate.Chop(3, new int[] { 1, 3, 5 }));
            Assert.AreEqual(2, karate.Chop(5, new int[] { 1, 3, 5 }));
            Assert.AreEqual(-1, karate.Chop(0, new int[] { 1, 3, 5 }));
            Assert.AreEqual(-1, karate.Chop(2, new int[] { 1, 3, 5 }));
            Assert.AreEqual(-1, karate.Chop(4, new int[] { 1, 3, 5 }));
            Assert.AreEqual(-1, karate.Chop(6, new int[] { 1, 3, 5 }));

            Assert.AreEqual(0, karate.Chop(1, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(1, karate.Chop(3, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(2, karate.Chop(5, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(3, karate.Chop(7, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(-1, karate.Chop(0, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(-1, karate.Chop(2, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(-1, karate.Chop(4, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(-1, karate.Chop(6, new int[] { 1, 3, 5, 7 }));
            Assert.AreEqual(-1, karate.Chop(8, new int[] { 1, 3, 5, 7 }));

            Assert.AreEqual(0, karate.Chop(1, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(1, karate.Chop(2, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(2, karate.Chop(3, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(3, karate.Chop(5, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(4, karate.Chop(7, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(5, karate.Chop(11, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(6, karate.Chop(13, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(7, karate.Chop(17, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(8, karate.Chop(19, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));

            Assert.AreEqual(-1, karate.Chop(-1, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(-1, karate.Chop(15, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
            Assert.AreEqual(-1, karate.Chop(30, new int[] { 1, 2, 3, 5, 7, 11, 13, 17, 19 }));
        }