.NET 中的高分辨率计时器 [英] High resolution timer in .NET
本文介绍了.NET 中的高分辨率计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对我的代码进行一些基本的分析,但发现 C# 中的 DateTime.Now 只有大约 16 毫秒的分辨率.一定有更好的时间保持构造,我还没有找到.
I'd like to do some basic profiling of my code, but found that the DateTime.Now in C# only have a resolution of about 16 ms. There must be better time keeping constructs that I haven't yet found.
推荐答案
下面是一段操作计时的示例代码:
Here is a sample bit of code to time an operation:
Dim sw As New Stopwatch()
sw.Start()
//Insert Code To Time
sw.Stop()
Dim ms As Long = sw.ElapsedMilliseconds
Console.WriteLine("Total Seconds Elapsed: " & ms / 1000)
最妙的是它也可以恢复.
And the neat thing is that it can resume as well.
Stopwatch sw = new Stopwatch();
foreach(MyStuff stuff in _listOfMyStuff)
{
sw.Start();
stuff.DoCoolCalculation();
sw.Stop();
}
Console.WriteLine("Total calculation time: {0}", sw.Elapsed);
System.Diagnostics.Stopwatch 类将如果您的系统上有高分辨率计数器,请使用高分辨率计数器.
The System.Diagnostics.Stopwatch class will use a high-resolution counter if one is available on your system.
这篇关于.NET 中的高分辨率计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文