ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
timer.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <arch/platform.h>
14 
15 #include <stdint.h>
16 #include <time.h>
17 
19 typedef uint_fast64_t timer_uint;
20 
28 static inline timer_uint timer_new(void);
29 
35 static inline timer_uint timer_value(timer_uint start);
36 
37 #ifdef __POSIX
38 #include <sys/time.h>
39 
40 static inline timer_uint timer_new(void)
41 {
42  struct timeval tmptv;
43  gettimeofday(&tmptv, NULL);
44  return (timer_uint)tmptv.tv_sec * 1000000U + tmptv.tv_usec;
45 }
46 
47 static inline timer_uint timer_value(timer_uint start)
48 {
49  return timer_new() - start;
50 }
51 
52 #endif
53 
54 #ifdef __WINDOWS
55 #include <windows.h>
56 
57 static timer_uint timer_perf_freq = 0;
58 
59 static inline timer_uint timer_new(void)
60 {
61  LARGE_INTEGER start_time;
62  QueryPerformanceCounter(&start_time);
63  return (timer_uint)start_time.QuadPart;
64 }
65 
66 
67 static inline timer_uint timer_value(timer_uint start)
68 {
69  if(unlikely(timer_perf_freq == 0)) {
70  LARGE_INTEGER perf;
71  QueryPerformanceFrequency(&perf);
72  timer_perf_freq = perf.QuadPart;
73  }
74  return (timer_new() - start) * 1000000U / timer_perf_freq;
75 }
76 
77 #endif
78 
88 #if defined(__x86_64__) || defined(__i386__)
89 #ifdef __WINDOWS
90 #include <intrin.h>
91 #else
92 #include <x86intrin.h>
93 #endif
94 
95 static inline timer_uint timer_hr_new(void)
96 {
97  return __rdtsc();
98 }
99 
100 #else
101 
102 static inline timer_uint timer_hr_new(void)
103 {
104  return timer_new();
105 }
106 
107 #endif
108 
115 {
116  return timer_hr_new() - start;
117 }
#define unlikely(exp)
Optimize the branch as likely not taken.
Definition: core.h:51
Determine on what OS we are compiling.
static timer_uint timer_hr_value(timer_uint start)
Compute a time interval measure using a previous timer_uint value.
Definition: timer.h:114
uint_fast64_t timer_uint
Definition: timer.h:19
static timer_uint timer_hr_new(void)
Start a high resolution, CPU dependent time interval measure.
Definition: timer.h:102
static timer_uint timer_value(timer_uint start)
Compute a time interval measure using a previous timer_uint value.
static timer_uint timer_new(void)
Get a new starting point for an time interval measure.