zoukankan      html  css  js  c++  java
  • CodeTimer

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    
    namespace IOCPerformanceTest.Core
    {
        public sealed class CodeTimer
        {
            public static void Initialize()
            {
                Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
                Thread.CurrentThread.Priority = ThreadPriority.Highest;
                Time("", 1, () => { });
            }
    
            public static void Time(string name, Action action)
            {
                Time(name, 1, action);
            }
    
            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();
                long cycleCount = GetCycleCount();
                for (int i = 0; i < iteration; i++) action();
                long 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 long GetCycleCount()
            {
                //ulong cycleCount = 0;
                //QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
                //return cycleCount;
                return GetCurrentThreadTimes();
            }
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime,
               out long lpExitTime, out long lpKernelTime, out long lpUserTime);
    
            private static long GetCurrentThreadTimes()
            {
                long l;
                long kernelTime, userTimer;
                GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
                   out userTimer);
                return kernelTime + userTimer;
            }
    
    
            //[DllImport("kernel32.dll")]
            //[return: MarshalAs(UnmanagedType.Bool)]
            //static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);
    
            [DllImport("kernel32.dll")]
            static extern IntPtr GetCurrentThread();
    
        }
    }
    

      

  • 相关阅读:
    centos8上安装中文字符集
    python使用p12个人证书发送S/MIME加密,签名邮件
    linux 下如何获取 cpu 温度
    [官网]Apache Log4j2 最新版安全提示 2.17.0
    电子书下载:Beginning Windows Phone 7 Development
    沙发的含义
    电子书下载:Windows Phone 7 Developer Guide: Building connected mobile applications with Microsoft Silverlight
    电子书下载:The Rails 3 Way, 2nd Edition
    Enfocus Pitstop 7.52 汉化破解版安装
    电子书下载:Professional Flash Mobile Development: Creating Android and iPhone Applications
  • 原文地址:https://www.cnblogs.com/RR-ghost/p/4238809.html
Copyright © 2011-2022 走看看