本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2009年04月23日 统计字数: 662字 阅读时间: 2分钟阅读 本文链接: https://soulteary.com/2009/04/23/c-queryperformancecounter-queryperformancefrequency.html ----- # [C++]用 QueryPerformanceFrequency 和 QueryPerformanceCounter 进行高精度计时 用 QueryPerformanceFrequency 和 QueryPerformanceCounter 进行高精度计时 [http://www.cppblog.com/bidepan2023/archive/2008/01/22/41627.html](http://www.cppblog.com/bidepan2023/archive/2008/01/22/41627.html) ```c void main() { LARGE_INTEGER lv; // 获取每秒多少CPU Performance Tick QueryPerformanceFrequency( &lv ); // 转换为每个Tick多少秒 double secondsPerTick = 1.0 / lv.QuadPart; for ( size_t i = 0; i < 100; ++i ) { // 获取CPU运行到现在的Tick数 QueryPerformanceCounter( &lv ); // 计算CPU运行到现在的时间 // 比GetTickCount和timeGetTime更加精确 double timeElapsedTotal = secondsPerTick * lv.QuadPart; cout.precision( 6 ); cout << fixed << showpoint << timeElapsedTotal << endl; //printf( "%lf ", timeElapsedTotal ) ; } } ```