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 | { |
---|
112 | LARGE_INTEGER counter; |
---|
113 | QueryPerformanceCounter(&counter); |
---|
114 | // return in usec |
---|
115 | return (long) (1000000*counter.QuadPart/(hrFreq.QuadPart)); |
---|
116 | } else { |
---|
117 | static struct _timeb mtime; |
---|
118 | _ftime(&mtime); |
---|
119 | |
---|
120 | return 1000 * (1000 * mtime.time + mtime.millitm); |
---|
121 | } |
---|
122 | #endif |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | // return time diff. in ms |
---|
127 | double |
---|
128 | timeDiff(long time1,long time2) // in ms |
---|
129 | { |
---|
130 | const double clk=1.0E-3; // ticks per second |
---|
131 | long t=time2-time1; |
---|
132 | |
---|
133 | return ((t<0)?-t:t)*clk; |
---|
134 | } |
---|
135 | |
---|
136 | char |
---|
137 | *timeString() |
---|
138 | { |
---|
139 | time_t t; |
---|
140 | time(&t); |
---|
141 | |
---|
142 | return ctime(&t); |
---|
143 | } |
---|