1 | #include <time.h> |
---|
2 | |
---|
3 | |
---|
4 | #ifdef HRTIME |
---|
5 | #include <hrtime.h> |
---|
6 | #endif |
---|
7 | |
---|
8 | #ifdef _WIN32 |
---|
9 | |
---|
10 | #include <windows.h> |
---|
11 | #include <sys/types.h> |
---|
12 | #include <sys/timeb.h> |
---|
13 | #include <time.h> |
---|
14 | #include <resource.h> |
---|
15 | |
---|
16 | #else |
---|
17 | |
---|
18 | #include <unistd.h> |
---|
19 | #include <sys/types.h> |
---|
20 | #include <sys/times.h> |
---|
21 | #include <sys/time.h> |
---|
22 | #include <sys/resource.h> |
---|
23 | |
---|
24 | #endif |
---|
25 | |
---|
26 | |
---|
27 | #include "Timers.h" |
---|
28 | |
---|
29 | /*real time or user time*/ |
---|
30 | |
---|
31 | #define REAL_TIME |
---|
32 | |
---|
33 | |
---|
34 | // global file streams |
---|
35 | /*ofstream debugs("debug.log");*/ |
---|
36 | |
---|
37 | static int hasHRTimer = 0; |
---|
38 | |
---|
39 | #ifdef _MSC_VER |
---|
40 | static LARGE_INTEGER hrFreq; |
---|
41 | #endif |
---|
42 | |
---|
43 | |
---|
44 | #ifdef HRTIME |
---|
45 | struct hrtime_struct *hrTimer = NULL; |
---|
46 | #endif |
---|
47 | |
---|
48 | void initTiming() |
---|
49 | { |
---|
50 | #ifdef _MSC_VER |
---|
51 | hasHRTimer = QueryPerformanceFrequency(&hrFreq); |
---|
52 | #else |
---|
53 | #ifdef HRTIME |
---|
54 | if (hrtime_is_present()) |
---|
55 | { |
---|
56 | hrtime_init(); |
---|
57 | hasHRTimer = (0 == get_hrtime_struct(0, &hrTimer)); |
---|
58 | debugs<<"Using UNIX hires timer"<<endl; |
---|
59 | } else |
---|
60 | { |
---|
61 | debugs<<"No UNIX hires timer"<<endl; |
---|
62 | } |
---|
63 | #endif |
---|
64 | #endif |
---|
65 | } |
---|
66 | |
---|
67 | void finishTiming() |
---|
68 | { |
---|
69 | #ifdef HRTIME |
---|
70 | if (hasHRTimer == 1) |
---|
71 | free_hrtime_struct(hrTimer); |
---|
72 | #endif |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | long |
---|
78 | getTime() |
---|
79 | { |
---|
80 | #ifndef _MSC_VER |
---|
81 | |
---|
82 | #ifdef REAL_TIME |
---|
83 | |
---|
84 | static struct timeval _tstart; |
---|
85 | static struct timezone tz; |
---|
86 | |
---|
87 | gettimeofday(&_tstart,&tz); |
---|
88 | return (long)(1000000*_tstart.tv_sec + _tstart.tv_usec); |
---|
89 | |
---|
90 | #else |
---|
91 | |
---|
92 | if (hasHRTimer == 0) { |
---|
93 | |
---|
94 | static struct rusage r; |
---|
95 | getrusage(RUSAGE_SELF,&r); |
---|
96 | return r.ru_utime.tv_usec+1000000*r.ru_utime.tv_sec; |
---|
97 | } else { |
---|
98 | #ifdef HRTIME |
---|
99 | hrtime_t dest; |
---|
100 | get_hrutime(hrTimer, &dest); |
---|
101 | return (long) (dest/500.0); |
---|
102 | #else |
---|
103 | return 0; |
---|
104 | #endif |
---|
105 | } |
---|
106 | #endif |
---|
107 | |
---|
108 | #else |
---|
109 | |
---|
110 | if (hasHRTimer == 1) { |
---|
111 | LARGE_INTEGER counter; |
---|
112 | QueryPerformanceCounter(&counter); |
---|
113 | // return in usec |
---|
114 | return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart)); |
---|
115 | } else { |
---|
116 | static struct _timeb mtime; |
---|
117 | _ftime(&mtime); |
---|
118 | |
---|
119 | return 1000*(1000*mtime.time + mtime.millitm); |
---|
120 | } |
---|
121 | #endif |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | // return time diff. in ms |
---|
126 | double |
---|
127 | timeDiff(long time1,long time2) // in ms |
---|
128 | { |
---|
129 | const double clk=1.0E-3; // ticks per second |
---|
130 | long t=time2-time1; |
---|
131 | |
---|
132 | return ((t<0)?-t:t)*clk; |
---|
133 | } |
---|
134 | |
---|
135 | char |
---|
136 | *timeString() |
---|
137 | { |
---|
138 | time_t t; |
---|
139 | time(&t); |
---|
140 | return ctime(&t); |
---|
141 | } |
---|