Reputation: 658
I need to calculate time difference in milliseconds on Linux (Ubuntu 14).
It needs to be independent from system time because the application may change it during the execution (it sets system time according to the data received from GPS).
I've checked the clock function and it doesn't work for us because it returns the processor time consumed by the program and we need the real time.
The sysinfo (as mentioned in this question) return seconds since boot and, again, we need milliseconds.
And reading from /proc/uptime (as mentioned in this question) seem to be slow according to our tests (considering we want milliseconds and this function is called repeatedly).
We can use C++11, but I think std::chrono is related to system time also (correct me if I'm wrong).
Is there any other method to accomplish this?
Our Performance Test (for /proc/uptime comparison), 1 million of repeated calls:
(not what we need since it depends on the system time)
#include <sys/time.h>
unsigned int GetMs(){
unsigned int ret = 0;
timeval ts;
gettimeofday(&ts,0);
static long long inici = 0;
if (inici==0){
inici = ts.tv_sec;
}
ts.tv_sec -= inici;
ret = (ts.tv_sec*1000 + (ts.tv_usec/1000));
return ret;
}
(not valid, returns ticks used by the application, not real time)
#include <time.h>
unsigned int GetMs(){
unsigned int ret = 0;
clock_t t;
t = clock();
ret = t / 1000;
return ret;
}
#include <fstream>
unsigned int GetMs(){
unsigned int ret = 0;
double uptime_seconds;
if (std::ifstream("/proc/uptime", std::ios::in) >> uptime_seconds) {
ret = (int) (1000 * uptime_seconds);
}
}
Results:
Upvotes: 3
Views: 2155
Reputation: 180595
What you want is std::chrono::steady_clock
Class
std::chrono::steady_clock
represents a monotonic clock. The time points of this clock cannot decrease as physical time moves forward. This clock is not related to wall clock time (for example, it can be time since last reboot), and is most suitable for measuring intervals.
If you need to support a C++98/03 environment you could also use boost:steady_clock
Upvotes: 9
Reputation: 140569
The low-level system primitive that does what you want is clock_gettime
with the CLOCK_MONOTONIC
"clockid".
CLOCK_MONOTONIC: Clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
(Being "affected by the incremental adjustments performed by adjtime(3) and NTP" is almost certainly what you want.)
This function is declared in <time.h>
and, with current versions of GNU libc, is accessible by default. With older versions you may need to define _POSIX_C_SOURCE
to a value greater than or equal to 200809L
before including any system headers (not just before including time.h
), and you may also need to link your program against -lrt
.
clock_gettime
returns a struct timespec
, which can express differences of a single nanosecond; the actual precision is usually somewhat less, but you ought to be able to count on millisecond precision, especially if a program that "disciplines" the system clock (e.g. ntpd
) is running.
Upvotes: 4