Example #1
0
        private static void Run(string name, int iterations)
        {
            var log = Console.Out;

            log.WriteLine("==== Run: {0} over {1:N0} iterations", name, iterations);

            {
                rand = new Random(0);
                var sw = new Stopwatch();
                sw.Start();
                var spoiler = LoopArrayFixed(iterations);
                sw.Stop();
                log.Write("LoopArrayFixed, {0:N0} iterations: ", iterations);
                log.WriteLine("{0} ({1:N0} ops/sec)", sw.Elapsed, sw.OpsPerSecond(iterations));
                log.WriteLine("Spoiler values: {0:N0}", spoiler);
            }

            {
                rand = new Random(0);
                var sw = new Stopwatch();
                sw.Start();
                var spoiler = LoopArrayFixed_ForLoop(iterations);
                sw.Stop();
                log.Write("LoopArrayFixed_ForLoop, {0:N0} iterations: ", iterations);
                log.WriteLine("{0} ({1:N0} ops/sec)", sw.Elapsed, sw.OpsPerSecond(iterations));
                log.WriteLine("Spoiler values: {0:N0}", spoiler);
            }

            {
                rand = new Random(0);
                var sw = new Stopwatch();
                sw.Start();
                var spoiler = LoopYieldFixed(iterations);
                sw.Stop();
                log.Write("LoopYieldFixed, {0:N0} iterations: ", iterations);
                log.WriteLine("{0} ({1:N0} ops/sec)", sw.Elapsed, sw.OpsPerSecond(iterations));
                log.WriteLine("Spoiler values: {0:N0}", spoiler);
            }
        }
 /// <summary>
 /// Calculate ops per second as a formatted string.
 /// </summary>
 public static string OpsPerSecond(int iterations, Stopwatch sw)
 {
     return string.Format("({0:N0}) ops/sec", sw.OpsPerSecond(iterations));
 }
        private static void RunInternal(bool isWarmup)
        {
            var spoiler = 0;

            {
                var iterations = isWarmup ? 1000 : 1000 * 1000 * 1000;
                var sw = new Stopwatch();
                sw.Start();
                for (var i = 0; i < iterations; i++)
                {
                    var z = new SomeClass(i);
                    spoiler += z.Value;
                }
                sw.Stop();
                Console.WriteLine("Ctor direct. Iterations: {0:N0}. Elapsed: {1}. Ops/sec: {2:N0}", iterations, sw.Elapsed, sw.OpsPerSecond(iterations));
            }

            {
                var iterations = isWarmup ? 1000 : 1000 * 1000 * 1000;
                var sw = new Stopwatch();
                sw.Start();
                for (var i = 0; i < iterations; i++)
                {
                    var z = SomeClassCtorMethod(i);
                    spoiler += z.Value;
                }
                sw.Stop();
                Console.WriteLine("Ctor via a method call. Iterations: {0:N0}. Elapsed: {1}. Ops/sec: {2:N0}", iterations, sw.Elapsed, sw.OpsPerSecond(iterations));
            }

            {
                // Fewer iterations for this as this runs for much longer
                var iterations = isWarmup ? 1000 : 100 * 1000 * 1000;
                var sw = new Stopwatch();
                sw.Start();
                for (var i = 0; i < iterations; i++)
                {
                    // Does not require any ctor. Ctor is not called.
                    var z = (SomeClass)FormatterServices.GetUninitializedObject(typeof(SomeClass));
                    spoiler += z.Value;
                }
                sw.Stop();
                Console.WriteLine("FormatterServices.GetUninitializedObject. Iterations: {0:N0}. Elapsed: {1}. Ops/sec: {2:N0}", iterations, sw.Elapsed, sw.OpsPerSecond(iterations));
            }

            {
                // Fewer iterations for this as this runs for much longer
                var iterations = isWarmup ? 1000 : 100 * 1000 * 1000;
                var sw = new Stopwatch();
                sw.Start();
                for (var i = 0; i < iterations; i++)
                {
                    // Requires default public ctor
                    var z = Activator.CreateInstance<SomeClass>();
                    spoiler += z.Value;
                }
                sw.Stop();
                Console.WriteLine("Activator.CreateInstance<SomeClass>. Iterations: {0:N0}. Elapsed: {1}. Ops/sec: {2:N0}", iterations, sw.Elapsed, sw.OpsPerSecond(iterations));
            }

            {
                // Fewer iterations for this as this runs for much longer
                var iterations = isWarmup ? 1000 : 100 * 1000 * 1000;
                var sw = new Stopwatch();
                sw.Start();
                for (var i = 0; i < iterations; i++)
                {
                    // Requires default public ctor
                    var z = (SomeClass)Activator.CreateInstance(typeof(SomeClass));
                    spoiler += z.Value;
                }
                sw.Stop();
                Console.WriteLine("Activator.CreateInstance. Iterations: {0:N0}. Elapsed: {1}. Ops/sec: {2:N0}", iterations, sw.Elapsed, sw.OpsPerSecond(iterations));
            }

            {
                var t = typeof(SomeClass);
                var ctor = t.GetConstructor(BindingFlags.Instance | BindingFlags.Public, binder: null, types: new Type[0], modifiers: new ParameterModifier[0]);
                
                // Fewer iterations for this as this runs for much longer
                var iterations = isWarmup ? 1000 : 100 * 1000 * 1000;
                var sw = new Stopwatch();
                sw.Start();
                for (var i = 0; i < iterations; i++)
                {
                    // Requires default public ctor
                    var z = (SomeClass)ctor.Invoke(new object[0]);
                    spoiler += z.Value;
                }
                sw.Stop();
                Console.WriteLine("Reflection invoke ctor. Iterations: {0:N0}. Elapsed: {1}. Ops/sec: {2:N0}", iterations, sw.Elapsed, sw.OpsPerSecond(iterations));
            }

            Console.WriteLine("Spoiler value: {0}", spoiler);
        }