找到c ++的执行时间 [英] find c++ execution time
问题描述
我很好奇,如果在C ++中有一个内置函数用于测量执行时间?
我目前正在使用Windows。在Linux中很容易...
I am curious if there is a build-in function in C++ for measuring the execution time? I am using Windows at the moment. In Linux it's pretty easy...
推荐答案
据我所知,Windows上最好的方法是使用 QueryPerformanceCounter
和 QueryPerformanceFrequency
。
The best way on Windows, as far as I know, is to use QueryPerformanceCounter
and QueryPerformanceFrequency
.
QueryPerformanceCounter (LARGE_INTEGER *)
将性能计数器的值放入传递的LARGE_INTEGER中。
QueryPerformanceCounter(LARGE_INTEGER*)
places the performance counter's value into the LARGE_INTEGER passed.
QueryPerformanceFrequency(LARGE_INTEGER *)
将性能计数器递增的频率放置到传递的LARGE_INTEGER中。
QueryPerformanceFrequency(LARGE_INTEGER*)
places the frequency the performance counter is incremented into the LARGE_INTEGER passed.
然后,通过在执行开始时记录计数器来找到执行时间,执行完成时计数器。从结束减去开始以获得计数器的更改,然后除以频率以获取时间(以秒为单位)。
You can then find the execution time by recording the counter as execution starts, and then recording the counter when execution finishes. Subtract the start from the end to get the counter's change, then divide by the frequency to get the time in seconds.
LARGE_INTEGER start, finish, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
// Do something
QueryPerformanceCounter(&finish);
std::cout << "Execution took "
<< ((finish.QuadPart - start.QuadPart) / (double)freq.QuadPart) << std::endl;
这篇关于找到c ++的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!