cannam@95
|
1 /*
|
cannam@95
|
2 * Copyright (c) 2001 Matteo Frigo
|
cannam@95
|
3 * Copyright (c) 2001 Massachusetts Institute of Technology
|
cannam@95
|
4 *
|
cannam@95
|
5 * This program is free software; you can redistribute it and/or modify
|
cannam@95
|
6 * it under the terms of the GNU General Public License as published by
|
cannam@95
|
7 * the Free Software Foundation; either version 2 of the License, or
|
cannam@95
|
8 * (at your option) any later version.
|
cannam@95
|
9 *
|
cannam@95
|
10 * This program is distributed in the hope that it will be useful,
|
cannam@95
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@95
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@95
|
13 * GNU General Public License for more details.
|
cannam@95
|
14 *
|
cannam@95
|
15 * You should have received a copy of the GNU General Public License
|
cannam@95
|
16 * along with this program; if not, write to the Free Software
|
cannam@95
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
cannam@95
|
18 *
|
cannam@95
|
19 */
|
cannam@95
|
20
|
cannam@95
|
21
|
cannam@95
|
22 #include "bench.h"
|
cannam@95
|
23 #include <stdio.h>
|
cannam@95
|
24
|
cannam@95
|
25 /*
|
cannam@95
|
26 * System-dependent timing functions:
|
cannam@95
|
27 */
|
cannam@95
|
28 #ifdef HAVE_SYS_TIME_H
|
cannam@95
|
29 #include <sys/time.h>
|
cannam@95
|
30 #endif
|
cannam@95
|
31
|
cannam@95
|
32 #ifdef HAVE_UNISTD_H
|
cannam@95
|
33 #include <unistd.h>
|
cannam@95
|
34 #endif
|
cannam@95
|
35
|
cannam@95
|
36 #ifdef HAVE_BSDGETTIMEOFDAY
|
cannam@95
|
37 #ifndef HAVE_GETTIMEOFDAY
|
cannam@95
|
38 #define gettimeofday BSDgettimeofday
|
cannam@95
|
39 #define HAVE_GETTIMEOFDAY 1
|
cannam@95
|
40 #endif
|
cannam@95
|
41 #endif
|
cannam@95
|
42
|
cannam@95
|
43 double time_min;
|
cannam@95
|
44 int time_repeat;
|
cannam@95
|
45
|
cannam@95
|
46 #if !defined(HAVE_TIMER) && (defined(__WIN32__) || defined(_WIN32) || defined(_WINDOWS) || defined(__CYGWIN__))
|
cannam@95
|
47 #include <windows.h>
|
cannam@95
|
48 typedef LARGE_INTEGER mytime;
|
cannam@95
|
49
|
cannam@95
|
50 static mytime get_time(void)
|
cannam@95
|
51 {
|
cannam@95
|
52 mytime tv;
|
cannam@95
|
53 QueryPerformanceCounter(&tv);
|
cannam@95
|
54 return tv;
|
cannam@95
|
55 }
|
cannam@95
|
56
|
cannam@95
|
57 static double elapsed(mytime t1, mytime t0)
|
cannam@95
|
58 {
|
cannam@95
|
59 LARGE_INTEGER freq;
|
cannam@95
|
60 QueryPerformanceFrequency(&freq);
|
cannam@95
|
61 return (((double) t1.QuadPart - (double) t0.QuadPart)) /
|
cannam@95
|
62 ((double) freq.QuadPart);
|
cannam@95
|
63 }
|
cannam@95
|
64
|
cannam@95
|
65 #define HAVE_TIMER
|
cannam@95
|
66 #endif
|
cannam@95
|
67
|
cannam@95
|
68
|
cannam@95
|
69 #if defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_TIMER)
|
cannam@95
|
70 typedef struct timeval mytime;
|
cannam@95
|
71
|
cannam@95
|
72 static mytime get_time(void)
|
cannam@95
|
73 {
|
cannam@95
|
74 struct timeval tv;
|
cannam@95
|
75 gettimeofday(&tv, 0);
|
cannam@95
|
76 return tv;
|
cannam@95
|
77 }
|
cannam@95
|
78
|
cannam@95
|
79 static double elapsed(mytime t1, mytime t0)
|
cannam@95
|
80 {
|
cannam@95
|
81 return ((double) t1.tv_sec - (double) t0.tv_sec) +
|
cannam@95
|
82 ((double) t1.tv_usec - (double) t0.tv_usec) * 1.0E-6;
|
cannam@95
|
83 }
|
cannam@95
|
84
|
cannam@95
|
85 #define HAVE_TIMER
|
cannam@95
|
86 #endif
|
cannam@95
|
87
|
cannam@95
|
88 #ifndef HAVE_TIMER
|
cannam@95
|
89 #error "timer not defined"
|
cannam@95
|
90 #endif
|
cannam@95
|
91
|
cannam@95
|
92 static double calibrate(void)
|
cannam@95
|
93 {
|
cannam@95
|
94 /* there seems to be no reasonable way to calibrate the
|
cannam@95
|
95 clock automatically any longer. Grrr... */
|
cannam@95
|
96
|
cannam@95
|
97 return 0.01;
|
cannam@95
|
98 }
|
cannam@95
|
99
|
cannam@95
|
100
|
cannam@95
|
101 void timer_init(double tmin, int repeat)
|
cannam@95
|
102 {
|
cannam@95
|
103 static int inited = 0;
|
cannam@95
|
104
|
cannam@95
|
105 if (inited)
|
cannam@95
|
106 return;
|
cannam@95
|
107 inited = 1;
|
cannam@95
|
108
|
cannam@95
|
109 if (!repeat)
|
cannam@95
|
110 repeat = 8;
|
cannam@95
|
111 time_repeat = repeat;
|
cannam@95
|
112
|
cannam@95
|
113 if (tmin > 0)
|
cannam@95
|
114 time_min = tmin;
|
cannam@95
|
115 else
|
cannam@95
|
116 time_min = calibrate();
|
cannam@95
|
117 }
|
cannam@95
|
118
|
cannam@95
|
119 static mytime t0[BENCH_NTIMERS];
|
cannam@95
|
120
|
cannam@95
|
121 void timer_start(int n)
|
cannam@95
|
122 {
|
cannam@95
|
123 BENCH_ASSERT(n >= 0 && n < BENCH_NTIMERS);
|
cannam@95
|
124 t0[n] = get_time();
|
cannam@95
|
125 }
|
cannam@95
|
126
|
cannam@95
|
127 double timer_stop(int n)
|
cannam@95
|
128 {
|
cannam@95
|
129 mytime t1;
|
cannam@95
|
130 BENCH_ASSERT(n >= 0 && n < BENCH_NTIMERS);
|
cannam@95
|
131 t1 = get_time();
|
cannam@95
|
132 return elapsed(t1, t0[n]);
|
cannam@95
|
133 }
|
cannam@95
|
134
|