微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
{
    // 添加性能观察
    static void AddWatch(Action del)
    {
        AddWatch("执行耗费时间: {0}s",del);
    }
    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
        {
            )
            {
                del();
            }
        }
    }
}

 

使用方法

GameSetting { 初始化 Init() { PerformanceHelper.AddWatch(Init Gamesetting :{0}sfig); } LoadConfig() { Todo 加载配置表 } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐