在.NET高分辨率定时器 [英] High resolution timer in .NET

查看:212
本文介绍了在.NET高分辨率定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望做我的code一些基本的分析,却发现DateTime.Now在C#中只有约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.

推荐答案

下面是code样本位时的操作:

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

href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx">秒表类将使用高分辨率计数器,如果一个是

The Stopwatch class will use a high-resolution counter if one is available on your system.

这篇关于在.NET高分辨率定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆