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