• 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();
        }
    }
  • 相关阅读:
    [转贴]35岁之前成功12条法则
    any type,any name
    The quick brown fox jumps over the lazy dog.
    [总结]软件工程师笔试题目(C++)
    [转]IOCP介绍
    A simple IOCP Server/Client Class
    Flash for Linux
    看看你是否需要更新SYMBOL文件了??
    [转贴]The Code Project Visual C++ Forum FAQ
    (搜集)一些少走弯路的话语+参考信息
  • 原文地址:https://www.cnblogs.com/voidobject/p/6251395.html
Copyright © 2020-2023  润新知