zoukankan      html  css  js  c++  java
  • CodeTimer

    
    namespace Microshaoft
    {
        using System;
        using System.Diagnostics;
        using System.Threading;
        using System.Threading.Tasks;
        using System.Runtime.InteropServices;
        public static class CodeTimer
        {
            public static void Initialize()
            {
                Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
                Thread.CurrentThread.Priority = ThreadPriority.Highest;
                Time("", 1, () => { });
            }
            public static void ParallelTime(string name, int iteration, int maxDegreeOfParallelism, Action action)
            {
                InternalIterationProcess
                        (
                            name
                            , iteration
                            , () =>
                                {
                                    Parallel.For
                                                (
                                                    0
                                                    , iteration
                                                    , new ParallelOptions()
                                                                {
                                                                    MaxDegreeOfParallelism = maxDegreeOfParallelism
                                                                    //, TaskScheduler = null
                                                                }
                                                    , i =>
                                                        {
                                                            action();
                                                        }
                                                );
                                }
                        );
            }
            private static void InternalIterationProcess(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();
                action();
                ulong cpuCycles = GetCycleCount() - cycleCount;
                watch.Stop();
                // 4.
                Console.ForegroundColor = currentForeColor;
                Console.WriteLine
                                (
                                    "{0}Time Elapsed:{0}{1}ms"
                                    , "\t"
                                    , watch.ElapsedMilliseconds.ToString("N0")
                                );
                Console.WriteLine
                                (
                                    "{0}CPU Cycles:{0}{1}"
                                    , "\t"
                                    , cpuCycles.ToString("N0")
                                );
                // 5.
                for (int i = 0; i <= GC.MaxGeneration; i++)
                {
                    int count = GC.CollectionCount(i) - gcCounts[i];
                    Console.WriteLine
                                (
                                    "{0}Gen{1}:{0}{0}{2}"
                                    , "\t"
                                    , i
                                    , count
                                );
                }
                Console.WriteLine();
            }
            public static void Time(string name, int iteration, Action action)
            {
                InternalIterationProcess
                                    (
                                        name
                                        , iteration
                                        , () =>
                                            {
                                                for (int i = 0; i < iteration; i++)
                                                {
                                                    action();
                                                }
                                            }
                                        );
            }
            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();
        }
    }
    
    
  • 相关阅读:
    linux上部署javaWeb项目
    Android 调试native的crash和anr
    你怎么知道你的网站K
    Win 10开门人类智慧的世界领先
    Preference如何增加在activity生命周期监听器
    智能指针模板,要管理动态分配的内存
    两分钟找到一些注意事项
    javascript---在自由落体实现
    URAL 1934 Black Spot --- 最短的简单修改
    最简单的ADABOOST人脸检测程序。COPY执行,前提是你配置OpenCV周围环境
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/2431640.html
Copyright © 2011-2022 走看看