本文介绍了如何获得在C ++中运行函数所花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过谷歌搜索尝试了一些代码:
I tried some codes by googling :
clock_t start, end;
start = clock();
//CODES GOES HERE
end = clock();
std::cout << end - start <<"\n";
std::cout << (double) (end-start)/ CLOCKS_PER_SEC;
但即使
std::cout << (double) (end-start)/ (CLOCKS_PER_SEC/1000.0 );
不知道为什么,但是当我在Java中得到类似的内容时:getCurrentTimeMillis()可以很好地工作.我希望它显示毫秒数,因为计算机可以这么快地进行计算.
Don't know why but when I get the similar in Java : getCurrentTimeMillis() it works well. I want it to show the milliseconds as maybe the computer compute so fast.
推荐答案
我认为不能保证clock
具有足够高的分辨率来分析您的功能.如果您想知道一个函数执行的速度,您应该将其运行数千次而不是一次,测量总耗时并取平均值.
I don't think it's guaranteed that clock
has a high enough resolution to profile your function. If you want to know how fast a function executes, you should run it maybe a few thousands times instead of once, measure the total time it takes and take the average.
这篇关于如何获得在C ++中运行函数所花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!