zoukankan      html  css  js  c++  java
  • C#添加测量运行时间

    使用范围

    使用模块化开发,每个模块都有初始化功能,初始化功能可能包括:加载配置表,初始化事件,初始化设置

    那么如果想测量每个模块的Init时间呢?Net框架已经提供了测量运行的方法

    System.Diagnostics

    System.Diagnostics 命名空间包含具有以下功能的类型:能让你与系统进程、事件日志和性能计数器之间进行交互。

    子命名空间包含具有以下功能的类型:与代码分析工具进行交互,支持协定,扩展对应用程序监控和检测的设计时支持,使用 Windows 事件跟踪 (ETW) 跟踪子系统来记录事件数据,在事件日志中进行读取和写入,收集性能数据,以及读取和写入调试符号信息。

    System.Diagnostics.Stopwatch

    提供一组方法和属性,可用于准确地测量运行时间。

    MSDN:https://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch(v=vs.110).aspx

    Help组件

    public class PerformanceHelper
    {
        // 添加性能观察
        public static void AddWatch(Action del)
        {
            AddWatch("执行耗费时间: {0}s", del);
        }
        public static void AddWatch(string outputStr, Action del)
        {
            if (Debug.isDebugBuild)//判断是否检测运行时间
            {
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start(); //  开始监视代码运行时间
    
                if (del != null)
                {
                    del();
                }
    
                stopwatch.Stop(); //  停止监视
                TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间
                double millseconds = timespan.TotalMilliseconds;//总毫秒数
                decimal seconds = (decimal)millseconds / 1000m;
                Debug.Log(string.Format(outputStr, seconds.ToString("F7"))); // 7位精度
            }
            else
            {
                if (del != null)
                {
                    del();
                }
            }
        }
    }

    使用方法

    public class GameSetting
    {
        //初始化
        public void Init()
        {
            PerformanceHelper.AddWatch("Init Gamesetting :{0}s",LoadConfig);
        }
    
        public void LoadConfig()
        {
            //TODO 加载配置表
        }
    }
  • 相关阅读:
    windowswindows/windowslinux间文件远程传输
    GNUstep ObjectC Ubuntu
    Linux日常问题处理集
    上网实用技巧
    ubuntu host VirtualBox xp guest networking
    平衡组/递归匹配
    Linux在线词典
    正则表达式的构造摘要
    Microsoft .NET Pet Shop 4 架构与技术分析
    ASP.net 2.0资料吐血收藏(^_^) (转)
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/5153179.html
Copyright © 2011-2022 走看看