get_hrusec, get_sec_fromepoch, get_nsec_fromepoch - timestamp for interval measurements
#include <sys/types.h> #include <sys/system_stats.h> void get_hrusec(hrtime_t *usec); void get_sec_fromepoch(memtime_sec_t *sec); void get_nsec_fromepoch(hrtime_t *nsec);
These functions are provided for performance reasons. The overhead of reading time using these functions is minimum as they return time read from memory. No system calls are involved in get_* functions.
The get_hrusec() function returns high-resolution real time which is expressed as microseconds since some arbitrary time in the past. It is not correlated in any way to the time of day, and thus is not subject to resetting or drifting by way of adjtime(2) or settimeofday(3C). This high-resolution time is suited for performance measurement. Although the best time accuracy it gives is 1 millisecond unlike gethrtime(3C).
The get_sec_fromepoch() and get_nsec_fromepoch() functions return time expressed in elapsed seconds from epoch(January 1, 1970) and elapsed nanoseconds from epoch respectively. Time returned by both these functions are subject to setting or drifting by adjtime(2) or settimeofday(3C) calls. Note that the accuracy of nsecs returned by get_nsec_fromepoch() is 1 millisecond.
#include <stdio.h> #include <unistd.h> #include <sys/system_stats.h> #include <time.h> void main() { hrtime_t hres1, hres2; memtime_sec_t sec; get_hrusec(&hres1); sleep(1); get_hrusec(&hres2); printf("Slept for %lld microseconds\n", (hres2 - hres1)); get_sec_fromepoch(&sec); printf("Current time in seconds : %lld\n", sec); get_nsec_fromepoch(&hres1); printf("Current time in nanoseconds: %lld\n", hres1); }
See attributes(5) for descriptions of the following attributes:
|