GetLocalTime,Kernel32.dll,windows 7的准确性是多少
本文关键字:准确性 多少 windows Kernel32 dll GetLocalTime | 更新日期: 2023-09-27 18:25:10
我想得到一天中最准确的时间(1ms的准确度会很好)。我看到了这个使用GetLocalTime:的例子
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public class SystemTime
{
public ushort year;
public ushort month;
public ushort weekday;
public ushort day;
public ushort hour;
public ushort minute;
public ushort second;
public ushort millisecond;
}
public class LibWrap
{
[DllImport("Kernel32.dll")]
public static extern void GetLocalTime([In, Out] SystemTime st);
}
public class App
{
public static void Main()
{
SystemTime st = new SystemTime();
LibWrap.GetLocalTime(st);
Console.Write("{0}:{1}:{2}.{3}", st.hour, st.minute, st.second, st.millisecond);
Console.ReadKey();
}
}
它准确多少?
很可能是计时器的准确性。默认情况下为16ms(在较新版本的windows上),使用timeBeginPeriod(1)
可以减少到1ms(可能会增加计算机的功耗)。
我所知道的窗口上的所有"绝对时间"功能都以定时器的精度工作。所以我预计它不会比DateTime.Now
更好或更糟。
相对时间函数(StopWatch
,QueryPerformanceCounter
)具有更高的精度,但没有给出有意义的绝对时间。它们还可能遇到跨多个CPU核心的同步问题。
但你为什么不测试一下呢:写一个小程序,读取时间,并在时间增加时打印出来。对DateTime.Now
执行相同操作。并进行比较。