comparison src/fftw-3.3.3/libbench2/timer.c @ 10:37bf6b4a2645

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