Under Windows there are some handy functions like QueryPerformanceCounter
from mmsystem.h
to create a high resolution timer. Is there something similar for Linux?
The High Resolution Timers system allows a user space program to be wake up from a timer event with better accuracy, when using the POSIX timer APIs. Without this system, the best accuracy that can be obtained for timer events is 1 jiffy. This depends on the setting of HZ in the kernel.
This patch introduces a new subsystem for high-resolution kernel timers. One might ask the question: we already have a timer subsystem (kernel/timers.
There is a new type, ktime_t, which is used to store a time value in nanoseconds. This type, found in <linux/ktime. h>, is meant to be used as an opaque structure. And, interestingly, its definition changes depending on the underlying architecture.
It's been asked before here -- but basically, there is a boost ptime function you can use, or a POSIX clock_gettime() function which can serve basically the same purpose.
For Linux (and BSD) you want to use clock_gettime().
#include <sys/time.h> int main() { timespec ts; // clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux }
See: This answer for more information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With