1

我通过谷歌搜索尝试了一些代码:

clock_t start, end;
start = clock();
//CODES GOES HERE
end = clock();
std::cout << end - start <<"\n";
std::cout << (double) (end-start)/ CLOCKS_PER_SEC;

但结果经过的时间始终为 0,即使使用

std::cout << (double) (end-start)/ (CLOCKS_PER_SEC/1000.0 );

不知道为什么,但是当我在 Java 中得到类似的东西时: getCurrentTimeMillis() 它运行良好。我希望它显示毫秒,因为计算机计算速度可能如此之快。

4

3 回答 3

1

我认为不能保证clock具有足够高的分辨率来分析您的功能。如果你想知道一个函数的执行速度有多快,你应该运行它几千次而不是一次,测量它所花费的总时间并取平均值。

于 2011-03-12T14:24:57.627 回答
1
#include <boost/progress.hpp>

int main()
{
    boost::progress_timer timer;
    // code to time goes here
}

This will print out the time it took to run main. You can place your code in scopes to time several parts, i.e. { boost::progress_timer timer; ... }.

于 2011-03-12T16:14:21.757 回答
0

这个问题与您的问题有些相似:Timing a function in a C++ program that runs on Linux

看看这个答案

于 2011-03-12T15:21:32.237 回答