1gethrtime(3C) Standard C Library Functions gethrtime(3C)
2
3
4
6 gethrtime, gethrvtime - get high resolution time
7
9 #include <sys/time.h>
10
11 hrtime_t gethrtime(void);
12
13
14 hrtime_t gethrvtime(void);
15
16
18 The gethrtime() function returns the current high-resolution real time.
19 Time is expressed as nanoseconds since some arbitrary time in the past;
20 it is not correlated in any way to the time of day, and thus is not
21 subject to resetting or drifting by way of adjtime(2) or settimeof‐
22 day(3C). The hi-res timer is ideally suited to performance measurement
23 tasks, where cheap, accurate interval timing is required.
24
25
26 The gethrvtime() function returns the current high-resolution LWP vir‐
27 tual time, expressed as total nanoseconds of execution time.
28
29
30 The gethrtime() and gethrvtime() functions both return an hrtime_t,
31 which is a 64-bit (long long) signed integer.
32
34 The following code fragment measures the average cost of getpid(2):
35
36 hrtime_t start, end;
37 int i, iters = 100;
38
39 start = gethrtime();
40 for (i = 0; i < iters; i++)
41 getpid();
42 end = gethrtime();
43
44 printf("Avg getpid() time = %lld nsec\n", (end − start) / iters);
45
46
48 See attributes(5) for descriptions of the following attributes:
49
50
51
52
53 ┌─────────────────────────────┬─────────────────────────────┐
54 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
55 ├─────────────────────────────┼─────────────────────────────┤
56 │MT-Level │MT-Safe │
57 └─────────────────────────────┴─────────────────────────────┘
58
60 proc(1), adjtime(2), gettimeofday(3C), settimeofday(3C), attributes(5)
61
63 Although the units of hi-res time are always the same (nanoseconds),
64 the actual resolution is hardware dependent. Hi-res time is guaranteed
65 to be monotonic (it won't go backward, it won't periodically wrap) and
66 linear (it won't occasionally speed up or slow down for adjustment,
67 like the time of day can), but not necessarily unique: two sufficiently
68 proximate calls may return the same value.
69
70
71
72SunOS 5.11 7 Sep 2004 gethrtime(3C)