tags:

views:

494

answers:

3

I'm looking for an equivalent to GetTickCount() on Linux.

Presently I am using Python's time.time() which presumably calls through to gettimeofday(). My concern is that the time returned (the unix epoch), may change erratically if the clock is messed with, such as by NTP. A simple process or system wall time, that only increases positively at a constant rate would suffice.

Does any such time function in C or Python exist?

A: 

Yes, the kernel has high-resolution timers but it is differently. I would recommend that you look at the sources of any odd project that wraps this in a portable manner.

From C/C++ I usually #ifdef this and use gettimeofday() on Linux which gives me microsecond resolution. I often add this as a fraction to the seconds since epoch I also receive giving me a double.

Dirk Eddelbuettel
+4  A: 

You can use CLOCK_MONOTONIC e.g. in C:

struct timespec ts;
if(clock_gettime(CLOCK_MONOTONIC,&ts) != 0) {
 //error
}

See this question for a Python way - http://stackoverflow.com/questions/1205722/how-do-i-get-monotonic-time-durations-in-python

nos
Brilliant. Thanks for the link also.
Matt Joiner
And on very old Linux systems you can use /proc/uptime as a monotonic time source. Clunky but works.
Zan Lynx
There's a note on the manpage: `CLOCK_MONOTONIC_RAW` (since Linux 2.6.28; Linux-specific) Similar to `CLOCK_MONOTONIC`, but provides access to a raw hard‐ ware-based time that is not subject to NTP adjustments. Does this mean that `CLOCK_MONOTONIC_RAW` is more appropriate?
Matt Joiner
A: 

You should use: clock_gettime(CLOCK_MONOTONIC, &tp);. This call is not affected by the adjustment of the system time just like GetTickCount() on Windows.

macgarden