本文介紹了精確的時間測量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我在 C++ 中使用 time.h 來測量函數的計時.
I'm using time.h in C++ to measure the timing of a function.
clock_t t = clock();
someFunction();
printf("
Time taken: %.4fs
", (float)(clock() - t)/CLOCKS_PER_SEC);
但是,我總是將時間設為 0.0000.clock() 和 t 在單獨打印時具有相同的值.我想知道是否有辦法在 C++ 中精確測量時間(可能以納秒為單位).我使用的是 VS2010.
however, I'm always getting the time taken as 0.0000. clock() and t when printed separately, have the same value. I would like to know if there is way to measure the time precisely (maybe in the order of nanoseconds) in C++ . I'm using VS2010.
推薦答案
我通常使用 QueryPerformanceCounter
函數.
I usually use the QueryPerformanceCounter
function.
示例:
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER t1, t2; // ticks
double elapsedTime;
// get ticks per second
QueryPerformanceFrequency(&frequency);
// start timer
QueryPerformanceCounter(&t1);
// do something
...
// stop timer
QueryPerformanceCounter(&t2);
// compute and print the elapsed time in millisec
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
這篇關于精確的時間測量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!