为什么我的c++基于 Clock() 的函数返回负值?

发布于 2024-10-12 16:46:23 字数 499 浏览 9 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怂人 2024-10-19 16:46:23

clock() 返回时钟数自程序启动以来已过去了多少时间。如果要将时钟返回的值转换为秒,请除以CLOCKS_PER_SEC(反之亦然)。

只有一个陷阱,作为程序执行开始的时钟所使用的初始参考时刻可能因平台而异。要计算程序的实际处理时间,应将时钟返回的值与初始调用时钟返回的值进行比较。

编辑

拉斯曼非常友善地在评论中发布了其他陷阱。我将它们放在这里以供将来参考。

  1. 在其他几个实现中,clock() 返回的值还包括通过 wait(2)(或另一个等待)收集其状态的任何子项的时间。 - 类型调用)。 Linux 不将等待子进程的时间包含在 clock() 返回的值中。

  2. 请注意,时间可以循环。在 CLOCKS_PER_SEC 等于 1000000 [POSIX 规定] 的 32 位系统上,此函数将大约每 72 分钟返回相同的值。

EDIT2

经过一段时间的混乱,这是我的便携式(Linux/Windows)msleep。但要小心,我没有 C/C++ 经验,很可能会包含有史以来最愚蠢的错误。

#ifdef _WIN32

#include <windows.h>
#define msleep(ms) Sleep((DWORD) ms)

#else

#include <unistd.h>
inline void msleep(unsigned long ms) {
    while (ms--) usleep(1000);
}

#endif

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 by CLOCKS_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.

  1. On several other implementations, the value returned by clock() also includes the times of any children whose status has been collected via wait(2) (or another wait-type call). Linux does not include the times of waited-for children in the value returned by clock().

  2. 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.

#ifdef _WIN32

#include <windows.h>
#define msleep(ms) Sleep((DWORD) ms)

#else

#include <unistd.h>
inline void msleep(unsigned long ms) {
    while (ms--) usleep(1000);
}

#endif
十二 2024-10-19 16:46:23

你错过了 * (指针) ,
你的参数是指针(clock_t变量的地址)
所以,你的代码必须修改::

return((double(*end - *start)/CLOCKS_PER_SEC)*1000);

You missed * (pointer) ,
Your argument is pointer (address of clock_t variable)
so, Your code must be modified::

return((double(*end - *start)/CLOCKS_PER_SEC)*1000);
没有伤那来痛 2024-10-19 16:46:23

在 windows 下,您可以使用:

VOID WINAPI Sleep(
  __in  DWORD dwMilliseconds
);

在 linux 下,您将要使用:

#include <unistd.h>
unsigned int sleep(unsigned int seconds);

注意参数差异 - windows 下为毫秒,linux 下为秒。

Under windows, you can use:

VOID WINAPI Sleep(
  __in  DWORD dwMilliseconds
);

In linux, you will want to use:

#include <unistd.h>
unsigned int sleep(unsigned int seconds);

Notice the parameter difference - milliseconds under windows and seconds under linux.

一桥轻雨一伞开 2024-10-19 16:46:23

我的方法依赖于:

int gettimeofday(struct timeval *tv, struct timezone *tz); ,

它给出了自纪元以来的秒数和微秒数。根据 man 页面:

The tv argument is a struct timeval (as specified in <sys/time.h>):

           struct timeval {
               time_t      tv_sec;     /* seconds */
               suseconds_t tv_usec;    /* microseconds */
           };

所以我们开始:

#include <sys/time.h>

#include <iostream>
#include <iomanip>

static long myclock()
{
    struct timeval tv; 
    gettimeofday(&tv, NULL);
    return (tv.tv_sec * 1000000) + tv.tv_usec;
}

double getRuntime(long* end, long* start)
{
    return (*end - *start);
}

void doWork()
{
    sleep(3);
}

int main(void)
{
    long start = myclock();
    doWork();
    long end = myclock();

    std::cout << "Time elapsed: " << std::setprecision(6) << getRuntime(&end, &start)/1000.0 << " miliseconds" << std::endl;
    std::cout << "Time elapsed: " << std::setprecision(3) << getRuntime(&end, &start)/1000000.0 << " seconds" << std::endl;

    return 0;
}

输出:

Time elapsed: 3000.08 miliseconds
Time elapsed: 3 seconds

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:

The tv argument is a struct timeval (as specified in <sys/time.h>):

           struct timeval {
               time_t      tv_sec;     /* seconds */
               suseconds_t tv_usec;    /* microseconds */
           };

So here we go:

#include <sys/time.h>

#include <iostream>
#include <iomanip>

static long myclock()
{
    struct timeval tv; 
    gettimeofday(&tv, NULL);
    return (tv.tv_sec * 1000000) + tv.tv_usec;
}

double getRuntime(long* end, long* start)
{
    return (*end - *start);
}

void doWork()
{
    sleep(3);
}

int main(void)
{
    long start = myclock();
    doWork();
    long end = myclock();

    std::cout << "Time elapsed: " << std::setprecision(6) << getRuntime(&end, &start)/1000.0 << " miliseconds" << std::endl;
    std::cout << "Time elapsed: " << std::setprecision(3) << getRuntime(&end, &start)/1000000.0 << " seconds" << std::endl;

    return 0;
}

Outputs:

Time elapsed: 3000.08 miliseconds
Time elapsed: 3 seconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文