C# 中如何精确地测量一段逻辑的执行时间

C#中提供了一个类Stopwatch,可以实现这个需求,当然了,不通过这个类也可以实现类似的功能。下面简单介绍一下使用方法,直接上代码:

1 Stopwatch stopwatch = new Stopwatch();
2 stopwatch.Start();
3 //TODO        
4 stopwatch.Stop();
5 int totalSeconds = (int)(stopwatch.ElapsedMilliseconds / 1000f);
6 int minutes = (int)(totalSeconds / 60f);
7 int seconds = totalSeconds % 60;
8 string str = string.Format("耗时: {0} 分 {1} 秒。", minutes, seconds);

 

posted @ 2020-05-09 18:52  小·糊涂仙  阅读(195)  评论(0编辑  收藏  举报