当前位置: 首页>>代码示例>>C#>>正文


C# Stopwatch.OpsPerSecond方法代码示例

本文整理汇总了C#中Stopwatch.OpsPerSecond方法的典型用法代码示例。如果您正苦于以下问题:C# Stopwatch.OpsPerSecond方法的具体用法?C# Stopwatch.OpsPerSecond怎么用?C# Stopwatch.OpsPerSecond使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Stopwatch的用法示例。


在下文中一共展示了Stopwatch.OpsPerSecond方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        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);
            }
        }
开发者ID:ppanyukov,项目名称:PerfMicroBenchmarks,代码行数:39,代码来源:Program.cs

示例2: OpsPerSecond

 /// <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));
 }
开发者ID:ppanyukov,项目名称:PerfMicroBenchmarks,代码行数:7,代码来源:BenchmarkTests.cs

示例3: RunInternal

        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);
        }
开发者ID:ppanyukov,项目名称:PerfMicroBenchmarks,代码行数:95,代码来源:ObjectCreationBenchmarks.cs


注:本文中的Stopwatch.OpsPerSecond方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。