user697111
user697111

Reputation: 2262

cross platform high res timer in C?

I'm trying to learn c by porting one of my apps. I'm looking for a high resolution timer that works on linux, mac and windows.

Trying to keep with things that are ANSI C99. I'm using mingw and gcc, so any GNU libs should also be fine?

I looked at time.h; but everything I read warns about clock (cpu ticks) isn't reliable across platforms nor can you get "real time" (instead of cpu time) less than 1 second resolution.

Is there any libs like Boost for C?

Upvotes: 3

Views: 1488

Answers (2)

Matt Kline
Matt Kline

Reputation: 10507

Could you explain what you mean by clock() not being "reliable"? The function returns a value of type clock_t of how many ticks have passed since the start of the program. The number of ticks per second is defined by the macro CLOCKS_PER_SEC, which on most systems is 1000, meaning that each tick corresponds to a millisecond of time. Hopefully this resolution is sufficient.

Upvotes: 1

Charles Brunet
Charles Brunet

Reputation: 23160

Try gettimeofday from sys/time.h. Although this is not a hi-res timer, it's more accurate than time function.

Upvotes: 2

Related Questions