计时算法:C++ 中的clock() vs time()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12231166/
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
Timing algorithm: clock() vs time() in C++
提问by blaze
For timing an algorithm (approximately in ms), which of these two approaches is better:
对于算法计时(大约以毫秒为单位),这两种方法中哪一种更好:
clock_t start = clock();
algorithm();
clock_t end = clock();
double time = (double) (end-start) / CLOCKS_PER_SEC * 1000.0;
Or,
或者,
time_t start = time(0);
algorithm();
time_t end = time(0);
double time = difftime(end, start) * 1000.0;
Also, from some discussion in the C++ channel at Freenode, I know clock has a very bad resolution, so the timing will be zero for a (relatively) fast algorithm. But, which has better resolution time() or clock()? Or is it the same?
此外,从 Freenode 的 C++ 频道中的一些讨论中,我知道时钟的分辨率非常差,因此(相对)快速算法的时序将为零。但是,哪个具有更好的分辨率 time() 或 clock()?或者是一样的?
采纳答案by David Grayson
It depends what you want: time
measures the real time while clock
measures the processing time taken by the current process. If your process sleeps for any appreciable amount of time, or the system is busy with other processes, the two will be very different.
这取决于您想要什么:time
测量实时时间,同时clock
测量当前进程所花费的处理时间。如果您的进程休眠了任何可观的时间,或者系统正忙于其他进程,则两者将大不相同。
回答by Rapptz
<chrono>
would be a better library if you're using C++11.
<chrono>
如果您使用的是 C++11,那将是一个更好的库。
#include <iostream>
#include <chrono>
#include <thread>
void f()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
f();
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "f() took "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
<< " milliseconds\n";
}
Example taken from here.
示例取自此处。
回答by Plecharts
The time_t structure is probably going to be an integer, which means it will have a resolution of second.
time_t 结构可能是一个整数,这意味着它将具有秒的分辨率。
The first piece of code: It will only count the time that the CPU was doing something, so when you do sleep(), it will not count anything. It can be bypassed by counting the time you sleep(), but it will probably start to drift after a while.
第一段代码:它只会计算CPU正在做某事的时间,所以当你做sleep()时,它不会计算任何东西。它可以通过计算你 sleep() 的时间来绕过,但它可能会在一段时间后开始漂移。
The second piece: Only resolution of seconds, not so great if you need sub-second time readings.
第二部分:只有秒的分辨率,如果您需要亚秒级读数,则不是那么好。
For time readings with the best resolution you can get, you should do something like this:
要获得最佳分辨率的时间读数,您应该执行以下操作:
double getUnixTime(void)
{
struct timespec tv;
if(clock_gettime(CLOCK_REALTIME, &tv) != 0) return 0;
return (tv.tv_sec + (tv.tv_nsec / 1000000000.0));
}
double start_time = getUnixTime();
double stop_time, difference;
doYourStuff();
stop_time = getUnixTime();
difference = stop_time - start_time;
On most systems it's resolution will be down to few microseconds, but it can vary with different CPUs, and probably even major kernel versions.
在大多数系统上,它的分辨率将下降到几微秒,但它可能因不同的 CPU 甚至主要内核版本而异。
回答by Hanyu Ye
<chrono>
is the best. Visual Studio 2013 provides this feature. Personally, I have tried all the methods mentioned above. I strongly recommend you use the <chrono>
library. It can track the wall time and at the same time have a good resolution (much less than a second).
<chrono>
是最好的。Visual Studio 2013 提供了此功能。就个人而言,我已经尝试了上述所有方法。我强烈建议您使用该<chrono>
库。它可以跟踪墙壁时间,同时具有良好的分辨率(远小于一秒)。
回答by haripkannan
How about gettimeofday? When it is called it updates two structs, with timing information. Usually, the left hand struct is enough, which will carry time since the Epoch, 01-01-1970 00:00:00 (UTC). It can be used as follows:
gettimeofday 怎么样?当它被调用时,它会更新两个结构体,以及时间信息。通常,左手结构就足够了,它将携带自纪元 01-01-1970 00:00:00 (UTC) 以来的时间。它可以按如下方式使用:
#include <time.h>
struct timeval start;
double mtime, seconds, useconds;
gettimeofday(&start, NULL); //left hand struct is usually enough
seconds = start.tv_sec; //time in seconds
useconds = start.tv_usec; //time in microseconds