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 /n", timeElapsedTotal ) ;
}
}
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 /n", timeElapsedTotal ) ;
}
}
本文介绍了一种使用C++实现的高精度计时方法,通过调用Windows API函数QueryPerformanceFrequency和QueryPerformanceCounter来准确测量程序运行时间。这种方法比GetTickCount和timeGetTime更为精确。
2085

被折叠的 条评论
为什么被折叠?



