Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ Linux TickCount

Tags:

c++

linux

Is there a similar function like GetTickCount() for Linux?

I´ve tried out some other sutff, but they didn´t work at all.

So it should return the exact time in milliseconds since startup.

like image 388
SuperUser Avatar asked Sep 03 '25 03:09

SuperUser


1 Answers

/// Returns the number of ticks since an undefined time (usually system startup).
static uint64_t GetTickCountMs()
{
    struct timespec ts;

    clock_gettime(CLOCK_MONOTONIC, &ts);

    return (uint64_t)(ts.tv_nsec / 1000000) + ((uint64_t)ts.tv_sec * 1000ull);
}

Also useful...

/// Computes the elapsed time, in milliseconds, between two 'timespec'.
inline uint32_t TimeElapsedMs(const struct timespec& tStartTime, const struct timespec& tEndTime)
{
   return 1000*(tEndTime.tv_sec - tStartTime.tv_sec) +
          (tEndTime.tv_nsec - tStartTime.tv_nsec)/1000000;
}
like image 56
Simon Avatar answered Sep 05 '25 22:09

Simon