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