C#添加测量运行时间
发布时间:2021-05-21 06:58:17 所属栏目:大数据 来源: https://www.jb51.cc
导读:使用范围 使用模块化开发,每个模块都有初始化功能,初始化功能可能包括:加载配置表,初始化事件,初始化设置 那么如果想测量每个模块的Init时间呢?Net框架已经提供了测量运行的方法 System.Diagnostics System.Diagnostics 命名空间包含具有以下功能的类
使用范围使用模块化开发,每个模块都有初始化功能,初始化功能可能包括:加载配置表,初始化事件,初始化设置 那么如果想测量每个模块的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}s LoadConfig() { TODO 加载配置表 } }(编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |