开发者

Why is my c++ clock() based function returning a negative value?

开发者 https://www.devze.com 2023-02-06 06:48 出处:网络
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 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


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


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);


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号