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();
        }
    }
  • 相关阅读:
    hiho 1483 区间计数问题+二分答案
    Educational Codeforces Round 17 D dp
    Educational Codeforces Round 17 C 二分
    hiho 1461 暴力+随机
    (转)重置Mac OS X管理员密码
    解析Xcode把应用程序打包成ipa---解决打包完新版本itunes提示不是有效应用程序的问题
    SVN权限修复
    MAC安装SVNServer
    设置UIScrollView只可以水平或者竖直滚动
    (转)android之Fragment(官网资料翻译)
  • 原文地址:https://www.cnblogs.com/voidobject/p/6251395.html
Copyright © 2011-2022 走看看