为什么我的c++基于 Clock() 的函数返回负值?
我对 C++ 还是个新手,时钟函数是绝对的(意味着它计算你睡眠的时间),还是应用程序实际执行的时间?
我想要一种可靠的方法来产生 1 秒的精确间隔。我正在保存文件,所以我需要考虑这一点。我以毫秒为单位返回运行时间,然后在剩下的时间里睡觉。
有没有更准确或更简单的方法来做到这一点?
编辑: 我遇到的主要问题是我得到了一个负数:
double FCamera::getRuntime(clock_t* end, clock_t* start)
{
return((double(end - start)/CLOCKS_PER_SEC)*1000);
}
clock_t start = clock();
doWork();
clock_t end = clock();
double runtimeInMilliseconds = getRuntime(&end, &start);
它给了我一个负数,这是怎么回事?
瓦尔特
I am still new to C++, is the clock function absolute (meaning it counts how long you sleep for), or is it how much time the application actually executes for?
I want a reliable way to produce exact intervals of 1 second. I am saving files, so I need to account for that. I was returning the runtime for that in milliseconds, and then sleeping for the remainder.
Is there a more accurate or simpler way to do this?
EDIT:
The main problem I am having is that I am getting a negative number:
double FCamera::getRuntime(clock_t* end, clock_t* start)
{
return((double(end - start)/CLOCKS_PER_SEC)*1000);
}
clock_t start = clock();
doWork();
clock_t end = clock();
double runtimeInMilliseconds = getRuntime(&end, &start);
It's giving me a negative number, what's up with that?
Walter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
clock()
返回时钟数自程序启动以来已过去了多少时间。如果要将时钟返回的值转换为秒,请除以CLOCKS_PER_SEC
(反之亦然)。只有一个陷阱,作为程序执行开始的时钟所使用的初始参考时刻可能因平台而异。要计算程序的实际处理时间,应将时钟返回的值与初始调用时钟返回的值进行比较。
编辑
拉斯曼非常友善地在评论中发布了其他陷阱。我将它们放在这里以供将来参考。
在其他几个实现中,
clock()
返回的值还包括通过wait(2)
(或另一个等待)收集其状态的任何子项的时间。 - 类型调用)。 Linux 不将等待子进程的时间包含在clock()
返回的值中。请注意,时间可以循环。在 CLOCKS_PER_SEC 等于 1000000 [POSIX 规定] 的 32 位系统上,此函数将大约每 72 分钟返回相同的值。
EDIT2
经过一段时间的混乱,这是我的便携式(Linux/Windows)msleep。但要小心,我没有 C/C++ 经验,很可能会包含有史以来最愚蠢的错误。
clock()
returns the number of clock ticks elapsed since the program was launched. If you want to convert the value returned by clock into seconds divide byCLOCKS_PER_SEC
(and multiply for the other way around).There is just one pitfall, the initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call to clock.
EDIT
larsman has been so kind to post other pitfalls in the comments. I have included them here for future reference.
On several other implementations, the value returned by
clock()
also includes the times of any children whose status has been collected viawait(2)
(or another wait-type call). Linux does not include the times of waited-for children in the value returned byclock()
.Note that the time can wrap around. On a 32-bit system where CLOCKS_PER_SEC equals 1000000 [as mandated by POSIX] this function will return the same value approximately every 72 minutes.
EDIT2
After messing around a while here is my portable (Linux/Windows) msleep. Be wary though, I'm not experienced with C/C++ and will most likely contain the stupidest error ever.
你错过了 * (指针) ,
你的参数是指针(clock_t变量的地址)
所以,你的代码必须修改::
You missed * (pointer) ,
Your argument is pointer (address of clock_t variable)
so, Your code must be modified::
在 windows 下,您可以使用:
在 linux 下,您将要使用:
注意参数差异 - windows 下为毫秒,linux 下为秒。
Under windows, you can use:
In linux, you will want to use:
Notice the parameter difference - milliseconds under windows and seconds under linux.
我的方法依赖于:
int gettimeofday(struct timeval *tv, struct timezone *tz); ,
它给出了自纪元以来的秒数和微秒数。根据 man 页面:
所以我们开始:
输出:
My approach relies on:
int gettimeofday(struct timeval *tv, struct timezone *tz);
which gives the number of seconds and microseconds since the Epoch. According to the man pages:
So here we go:
Outputs: