C++ 精确的时间测量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14337278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
precise time measurement
提问by Abhishek Thakur
I'm using time.h in C++ to measure the timing of a function.
我在 C++ 中使用 time.h 来测量函数的计时。
clock_t t = clock();
someFunction();
printf("\nTime taken: %.4fs\n", (float)(clock() - t)/CLOCKS_PER_SEC);
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.
但是,我总是将时间设为 0.0000。clock() 和 t 在单独打印时具有相同的值。我想知道是否有办法在 C++ 中精确测量时间(可能以纳秒为单位)。我正在使用 VS2010。
回答by Axel Guilmin
C++11 introduced the chrono API, you can use to get nanoseconds :
C++11 引入了chrono API,你可以用它来获取纳秒:
auto begin = std::chrono::high_resolution_clock::now();
// code to benchmark
auto end = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count() << "ns" << std::endl;
For a more relevant value it is good to run the function several times and compute the average :
对于更相关的值,最好多次运行该函数并计算平均值:
auto begin = std::chrono::high_resolution_clock::now();
uint32_t iterations = 10000;
for(uint32_t i = 0; i < iterations; ++i)
{
// code to benchmark
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count();
std::cout << duration << "ns total, average : " << duration / iterations << "ns." << std::endl;
But remember the for
loop and assigning begin
and end
var use some CPU time too.
但是请记住for
循环和分配begin
以及end
var 也使用了一些 CPU 时间。
回答by Constantinius
I usually use the QueryPerformanceCounter
function.
我通常使用该QueryPerformanceCounter
功能。
example:
例子:
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;
回答by SChepurin
The following text, that i completely agree with, is quoted from Optimizing software in C++(good reading for any C++ programmer) -
我完全同意的以下文本引用自Optimizing software in C++(任何 C++ 程序员的好读物)-
The time measurements may require a very high resolution if time intervals are short. In Windows, you can use the
GetTickCount
orQueryPerformanceCounter
functions for millisecond resolution. A much higher resolution can be obtained with the time stamp counter in the CPU, which counts at the CPU clock frequency.
如果时间间隔很短,时间测量可能需要非常高的分辨率。在 Windows 中,您可以使用
GetTickCount
或QueryPerformanceCounter
函数进行毫秒分辨率。使用 CPU 中的时间戳计数器可以获得更高的分辨率,它以 CPU 时钟频率计数。
There is a problem that "the clock frequency may vary dynamically and that measurements are unstable due to interrupts and task switches."
存在“时钟频率可能动态变化,测量因中断和任务切换而不稳定”的问题。
回答by hmatar
In C or C++ I usually do like below. If it still fails you may consider using rtdsc functions
在 C 或 C++ 中,我通常会像下面这样。如果仍然失败,您可以考虑使用 rtdsc 函数
struct timeval time;
gettimeofday(&time, NULL); // Start Time
long totalTime = (time.tv_sec * 1000) + (time.tv_usec / 1000);
//........ call your functions here
gettimeofday(&time, NULL); //END-TIME
totalTime = (((time.tv_sec * 1000) + (time.tv_usec / 1000)) - totalTime);