• 简单性能计数器老赵版改良


    老赵版:http://www.cnblogs.com/JeffreyZhao/archive/2009/03/10/codetimer.html

    eaglet版:http://blog.csdn.net/eaglet/article/details/4213550

      通读了下他们的代码,老赵版在win7下是完美的,eaglet版~~~他在自己的续也说了GetThreadTimes获取线程实际运行时间上是有偏差的,我多次测试的确很不稳定。

    想来想去也没有什么办法可以改进的。。窝里是win7,公司的电脑是苦逼的xp。。也就是说我不能一个代码两个地方通吃。没办法改良下吧。

    随便鄙视还有framework2.0的公司真恶心

    View Code
    public static class CodeTimer
        {
            private static bool isQueryThreadCycleTime = false;
    
            public static void Initialize()
            {
                Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
                Thread.CurrentThread.Priority = ThreadPriority.Highest;
                if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6)
                {
                    isQueryThreadCycleTime = true;
                }
                Time("", 1, () => { });
            }
    
            public static void Time(string name, int iteration, Action action)
            {
                if (String.IsNullOrEmpty(name)) return;
    
                // warm up
                action();
    
                // 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("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
                Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));
    
                // 5.
                for (int i = 0; i <= GC.MaxGeneration; i++)
                {
                    int count = GC.CollectionCount(i) - gcCounts[i];
                    Console.WriteLine("\tGen " + i + ": \t\t" + count);
                }
    
                Console.WriteLine();
            }
    
            private static ulong GetCycleCount()
            {
                ulong cycleCount = 0;
                if (isQueryThreadCycleTime)
                {
                    QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
                }
                else
                {
                    ulong l;
                    ulong kernelTime, userTimer;
                    GetThreadTimes(GetCurrentThread(), out l, out l, out kernelTime,
                      out userTimer);
                    cycleCount = kernelTime + userTimer;
                } 
    
                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();
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool GetThreadTimes(IntPtr hThread, out ulong lpCreationTime,
               out ulong lpExitTime, out ulong lpKernelTime, out ulong lpUserTime);
        }
  • 相关阅读:
    css的em是根据什么来写的
    向一个对象数组里面添加新的属性 + 将一个对象数组数据拿出来变成另一个对象
    微信里iphone后退不刷新问题解决方案
    进到页面后input输入框自动获取焦点
    jquery checkbox反复调用attr('checked', true/false)只有第一次生效
    js promise中如何取到[[PromiseValue]]
    js 取得当天0点 / 23:59:59 时间
    jQuery获取包括当前元素的HTML
    C++ 实现 STL 标准库和算法(二)template 编程和迭代器粗解 实验楼笔记
    我现在怎么写博客笔记?
  • 原文地址:https://www.cnblogs.com/qionghua/p/2578618.html
Copyright © 2020-2023  润新知