zoukankan      html  css  js  c++  java
  • CodeTimer 代码性能计数器

    收集整理老赵 的”CodeTimer“。 用于测试代码性能。详见可参考 老赵原文 代码如下:

      

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace MongoDBDemo
    {
        public class CodeTimer
        {
            public static void Initialize()
            {
                Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
                Thread.CurrentThread.Priority = ThreadPriority.Highest;
                Time("", 1, () => { });
            }
    
    
            public static void Time(string name, int iteration, Action action)
            {
                if (String.IsNullOrEmpty(name)) return;
    
                // 1.
                ConsoleColor currentForeColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(name);
    
                // 2.
                GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
                int[] gcCounts = new int[GC.MaxGeneration + 1];
                for (int i = 0; i <= GC.MaxGeneration; i++)
                {
                    gcCounts[i] = GC.CollectionCount(i);
                }
    
                // 3.
                Stopwatch watch = new Stopwatch();
                watch.Start();
                ulong cycleCount = GetCycleCount();
                for (int i = 0; i < iteration; i++) action();
                ulong cpuCycles = GetCycleCount() - cycleCount;
                watch.Stop();
    
                // 4.
                Console.ForegroundColor = currentForeColor;
                Console.WriteLine("	Time Elapsed:	" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
                Console.WriteLine("	CPU Cycles:	" + cpuCycles.ToString("N0"));
    
                // 5.
                for (int i = 0; i <= GC.MaxGeneration; i++)
                {
                    int count = GC.CollectionCount(i) - gcCounts[i];
                    Console.WriteLine("	Gen " + i + ": 		" + count);
                }
    
                Console.WriteLine();
            }
    
            private static ulong GetCycleCount()
            {
                ulong cycleCount = 0;
                QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
                return cycleCount;
            }
    
            [DllImport("kernel32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);
    
            [DllImport("kernel32.dll")]
            static extern IntPtr GetCurrentThread();
        }
    }
  • 相关阅读:
    9
    8
    7
    lua开发和调试环境
    MeshFilter mesh vs sharedMesh
    几种方法验证unity是否为development build
    Unity SetActive Event
    利用Data Vault对数据仓库进行建模(二)
    程序员如何圆飞行梦想(一)
    利用Data vault对数据仓库建模
  • 原文地址:https://www.cnblogs.com/voidobject/p/6251395.html
Copyright © 2011-2022 走看看