C++计算执行时间错误
我正在尝试计算代码中函数的执行情况(需要一个多小时),并且我正在使用clock(),但我收到了一些错误,因为计算的时间为负数。我这样做是这样的:
long double time;
clock_t start, t_end;
t_start = clock();
algorithm->execute();
t_end = clock();
time = ((long double) t_end - t_start) / CLOCKS_PER_SEC;
cout << time << endl;
我做错了什么吗?
I'm trying to count the execution of function in my code (that takes over an hour), and I'm using clock(), but I'm getting some errors, since the time calculated is negative. I'm doing like this:
long double time;
clock_t start, t_end;
t_start = clock();
algorithm->execute();
t_end = clock();
time = ((long double) t_end - t_start) / CLOCKS_PER_SEC;
cout << time << endl;
Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CLOCKS_PER_SEC 为 1000000,clock() 返回一个带符号的 32 位值,因此在大约 36 分钟后将变为负值,并在大约 72 分钟后回绕。
请参阅http://www.guyrutenberg.com/2007/09 /22/profiling-code-using-clock_gettime 了解如何测量长执行时间的详细信息。
CLOCKS_PER_SEC is 1000000 and clock() returns a signed 32-bit value so will go negative after about 36 minutes and wrap after about 72 minutes.
See http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime for details on how to measure long execution times.
您是否检查过以确保对
clock()
的调用均未返回 -1?另一种方法是:
Have you checked to make sure neither call to
clock()
is returning -1?Another approach of doing this is:
我不是 100% 确定,但是你只将 t_end 转换为 long double 吗???
不应该是:
或者
???
I'm not 100% sure, but do you cast only t_end to a long double ???
Shouldn't it be:
or
???