C#检测代码块运行耗时多久

//监控性能需引用 System.Diagnostics
                    //荣达2022.11.24测试代码,检测打印耗时
                    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                    stopwatch.Start(); // 开始监代码运行时间
 //.........代码..................//
                    stopwatch.Stop(); // 停止监视
                    TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间
                    string seconds = timespan.TotalSeconds.ToString("#0.00000000 "); // 总秒数
                    string minutes = timespan.TotalMinutes.ToString("#0.00000000 "); // 总分钟
                    string milliseconds = timespan.TotalMilliseconds.ToString("#0.00000000 ");
                    //总毫秒数
                    string hours = timespan.TotalHours.ToString("#0.00000000 "); // 总小时

                    //将消耗时间显示到VS的”输出“窗口
                    Console.WriteLine(string.Format("消耗时间(秒):{0}", seconds));

需要检测代码块运行耗时多久时,可以使用此方法在控制台看到结果。非常好用!

相关推荐: C#反射将静态类数据反射到字典并读取不同分支的字典数据

然后根据switch case匹配的字典加载/// <summary> /// 获取警报的端口状态 /// </summary> private Dictionary<uint, Dictionary<string, uin…

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容