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);

浙公网安备 33010602011771号