Equivalent to GetTickCount () for Linux

I am looking for the equivalent of GetTickCount() on Linux.

I am currently using Python time.time() , which supposedly calls gettimeofday() . My concern is that the time returned (unix era) may change with an error if the clock is confused, such as NTP. A fairly simple operating time of a system or system, which only increases positively at a constant speed.

Is there such a time function in C or Python?

+27
c python linux time
Jun 02 '10 at 1:47 a.m.
source share
4 answers

You can use CLOCK_MONOTONIC for example. in C:

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

See this question for the Python path - How to get monotonous time durations in python?

+26
Jun 02 '10 at 13:55 on
source share

It works:

 #include <unistd.h> #include <time.h> uint32_t getTick() { struct timespec ts; unsigned theTick = 0U; clock_gettime( CLOCK_REALTIME, &ts ); theTick = ts.tv_nsec / 1000000; theTick += ts.tv_sec * 1000; return theTick; } 

yes, get_tick () is the foundation of my applications. It consists of one state machine for each "task", for example, it can perform multitasking without using threads, and Inter Process Communication can implement non-blocking delays.

+5
Jul 27 '14 at 12:48
source share

You should use: clock_gettime(CLOCK_MONOTONIC, &tp); . This call is independent of the system time setting, like GetTickCount () on Windows.

+4
Jun 02 2018-10-02T00:
source share

Yes, the kernel has high resolution timers, but it is different. I would recommend that you take a look at the sources of any odd project that carries this in a portable way.

From C / C ++, I usually #ifdef this and use gettimeofday() on Linux, which gives me permission in microseconds. I often add this as part of the seconds, since the era I also get gives me a double.

+1
Jun 02 '10 at 13:52
source share



All Articles