如何测量时间? [英] how to measure time?
问题描述
我知道有很多主题可以找到代码的经过时间,并且我已经阅读了很多,但是我很困惑,我决定使用高分辨率时钟并运行此代码以查看时钟滴答声每秒多少次 >
I know there are many topics for finding elapse time of a code and I have read many, but i really get confused I decided to use high resolution clock and ran this code to see how many times a second the clock ticks
cout << chrono::high_resolution_clock::period::den << endl;
output:10000000
然后我定义开始时间和结束时间如下
and then I defined the begin time and the end time as below
int main()
{
auto begin = std::chrono::high_resolution_clock::now();
.
.
.
.
auto end = std::chrono::high_resolution_clock::now();
cout<<"RUN TIME IS :"<<endl;
std::cout << std::chrono:: duration_cast <chrono:: nanoseconds>(end-begin).count() << "ns" << std::endl;
system("PAUSE");
//**********************************************************
return 0;
}
但是大多数时候输出是0,但是代码超过2000行我浪费了很多时间,但是问题还没有解决.因此,请让我知道这是确定经过时间的好方法吗?我的代码有什么问题呢?我正在使用VS2012 另一个问题是我想测量问题的计算时间,但是我不确定应该测量哪个时间(系统时间,用户时间或实时时间)? 预先谢谢你
but most of the time the output is 0, but the code is more than 2000lines I waste a lot of time,but the problem has not solved yet . So please let me know is it a good way to determine the elapse time ? And what is the wrong with my code ? I am using VS2012 another question is that i want to measure a computation time of a problem and I am not sure which one should i measure(system time , user time or real time) ? Thank you in advance
推荐答案
这是正确的...但是实际上并不需要时钟为高分辨率",这是实现的定义.
This is correct... But the clock is not actually required to be "high-resolution", that's implementation defined.
您可以考虑使用Windows API内置的替代方法:
You may consider using an alternate method, which is built into the Windows API:
按照此问题中的说明,检查QueryPerformanceCounter API.
Check out the QueryPerformanceCounter APIs, as explained in this question.
这篇关于如何测量时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!