1. 获取高精度时间戳
主要用于测试一段代码的执行时间。通过CPU的 tick-per-secs 来获取,可以精确到微秒甚至更高。比GetTickCount() API精确到毫秒要更加精确。
代码如下:
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds; LARGE_INTEGER Frequency; QueryPerformanceFrequency(&Frequency); QueryPerformanceCounter(&StartingTime); for ( int i = 0; i < 1000; i++) { printf("aaa\n"); } QueryPerformanceCounter(&EndingTime); ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart; ElapsedMicroseconds.QuadPart *= 1000; // ms ElapsedMicroseconds.QuadPart /= Frequency.QuadPart; printf("Time is %I64d ms\n", ElapsedMicroseconds.QuadPart);
2. Windows获取版本号
在Win8及其以后的版本,使用GetVersionEx() API 获取的版本号信息结构体是有问题的,可以使用如下的方法来兼容到Win10中。
到了Win10下就有微软建议的获取系统版本方法。可以参考MSDN。
OSVERSIONINFOEXW osver; ZeroMemory(&osver, sizeof(osver)); osver.dwOSVersionInfoSize = sizeof(osver); GetVersionExW((LPOSVERSIONINFOW)&osver); printf("GetVersionExW -- Major: %d, Minor: %d, Build: %d\n", osver.dwMajorVersion, osver.dwMinorVersion, osver.dwBuildNumber); // 尝试用RtlGetVersion获取 typedef LONG (NTAPI * PFN_RtlGetVersion)(OUT PRTL_OSVERSIONINFOW); PFN_RtlGetVersion _RtlGetVersion = (PFN_RtlGetVersion)GetProcAddress(GetModuleHandle(_T("ntdll")), "RtlGetVersion"); if (_RtlGetVersion) { RTL_OSVERSIONINFOEXW osver1; ex1.dwOSVersionInfoSize = sizeof(osver1); if (_RtlGetVersion((LPOSVERSIONINFOW)&osver1)>=0) { printf("RtlGetVersion -- Major: %d, Minor: %d, Build: %d\n", osver1.dwMajorVersion, osver1.dwMinorVersion, osver1.dwBuildNumber); } }3. XXX