DateTime.Ticks没有一项决议,AF为100 ns? [英] DateTime.Ticks does NOT have a resolution af 100 ns?
问题描述
可能重复:
C#DateTime.Now precision
有在.NET SO对于高分辨率定时几个问题,其中指出(如MSDN文档)的DateTime.Ticks具有100纳米秒resulution。
There are a few questions on SO regarding high-resolution timing in .NET where it is stated (as in the MSDN documentation) that the DateTime.Ticks has a resulution of 100 nano-seconds.
但是,这似乎不是在所有的情况在现实生活中。 如果我运行code以下,我希望它来生成线,那里的刻度值变化的每一行,其中很多线路都与相同Milisecond值。 但它没有 - 蜱值保持不变,直到milisecond值下一班,加上没什么时间戳的分辨率
But this seem not to be the case at all in real-life. If I run the code below, I would expect it to generate lines, where the Tick value varies for each line, where a lot of lines are with the the same Milisecond value. But it doesn't - the ticks value stays the same until next shift in milisecond value, adding nothing to the resolution of the time-stamp.
private static List<string> GetTimeLine(long iterations)
{
List<string> liste = new List<string>();
for (long i = 0; i <= iterations; i++)
{
liste.Add(DateTime.Now.Millisecond.ToString() + " - " + DateTime.Now.Ticks.ToString());
}
return liste;
}
static void Main(string[] args)
{
Console.WriteLine("Generating timeline");
guids = GetTimeLine(10000);
File.WriteAllLines(@"C:\Test\GUIDS.TXT", guids);
Console.WriteLine("File written - Press ENTER");
Console.ReadLine();
}
结果输出的例子:
Example of result output:
...
867 - 634940160118679615
...
867 - 634940160118679615
867 - 634940160118679615
867 - 634940160118679615
867 - 634940160118679615
867 - 634940160118679615
867 - 634940160118679615
867 - 634940160118679615
867 - 634940160118679615
867 - 634940160118679615
868 - 634940160118689616
868 - 634940160118689616
868 - 634940160118689616
868 - 634940160118689616
868 - 634940160118689616
868 - 634940160118689616
...
那么,什么是方法获取超过毫秒的分辨率在.NET?
So what is the method for getting more than miliseconds resolution in .NET?
推荐答案
测量经过时间在C#中最准确的方法是使用的秒表类。
The most accurate way of measuring elapsed time in C# is to use the Stopwatch class.
本采用微处理器的性能计数器,通过调用的 QueryPerformanceCounter的。
This uses the microprocessor's performance counter, implemented with calls to QueryPerformanceCounter.
这篇关于DateTime.Ticks没有一项决议,AF为100 ns?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!