cannam@95: /* cannam@95: * Copyright (c) 2001 Matteo Frigo cannam@95: * Copyright (c) 2001 Massachusetts Institute of Technology cannam@95: * cannam@95: * This program is free software; you can redistribute it and/or modify cannam@95: * it under the terms of the GNU General Public License as published by cannam@95: * the Free Software Foundation; either version 2 of the License, or cannam@95: * (at your option) any later version. cannam@95: * cannam@95: * This program is distributed in the hope that it will be useful, cannam@95: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@95: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@95: * GNU General Public License for more details. cannam@95: * cannam@95: * You should have received a copy of the GNU General Public License cannam@95: * along with this program; if not, write to the Free Software cannam@95: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@95: * cannam@95: */ cannam@95: cannam@95: cannam@95: #include "bench.h" cannam@95: #include cannam@95: cannam@95: /* cannam@95: * System-dependent timing functions: cannam@95: */ cannam@95: #ifdef HAVE_SYS_TIME_H cannam@95: #include cannam@95: #endif cannam@95: cannam@95: #ifdef HAVE_UNISTD_H cannam@95: #include cannam@95: #endif cannam@95: cannam@95: #ifdef HAVE_BSDGETTIMEOFDAY cannam@95: #ifndef HAVE_GETTIMEOFDAY cannam@95: #define gettimeofday BSDgettimeofday cannam@95: #define HAVE_GETTIMEOFDAY 1 cannam@95: #endif cannam@95: #endif cannam@95: cannam@95: double time_min; cannam@95: int time_repeat; cannam@95: cannam@95: #if !defined(HAVE_TIMER) && (defined(__WIN32__) || defined(_WIN32) || defined(_WINDOWS) || defined(__CYGWIN__)) cannam@95: #include cannam@95: typedef LARGE_INTEGER mytime; cannam@95: cannam@95: static mytime get_time(void) cannam@95: { cannam@95: mytime tv; cannam@95: QueryPerformanceCounter(&tv); cannam@95: return tv; cannam@95: } cannam@95: cannam@95: static double elapsed(mytime t1, mytime t0) cannam@95: { cannam@95: LARGE_INTEGER freq; cannam@95: QueryPerformanceFrequency(&freq); cannam@95: return (((double) t1.QuadPart - (double) t0.QuadPart)) / cannam@95: ((double) freq.QuadPart); cannam@95: } cannam@95: cannam@95: #define HAVE_TIMER cannam@95: #endif cannam@95: cannam@95: cannam@95: #if defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_TIMER) cannam@95: typedef struct timeval mytime; cannam@95: cannam@95: static mytime get_time(void) cannam@95: { cannam@95: struct timeval tv; cannam@95: gettimeofday(&tv, 0); cannam@95: return tv; cannam@95: } cannam@95: cannam@95: static double elapsed(mytime t1, mytime t0) cannam@95: { cannam@95: return ((double) t1.tv_sec - (double) t0.tv_sec) + cannam@95: ((double) t1.tv_usec - (double) t0.tv_usec) * 1.0E-6; cannam@95: } cannam@95: cannam@95: #define HAVE_TIMER cannam@95: #endif cannam@95: cannam@95: #ifndef HAVE_TIMER cannam@95: #error "timer not defined" cannam@95: #endif cannam@95: cannam@95: static double calibrate(void) cannam@95: { cannam@95: /* there seems to be no reasonable way to calibrate the cannam@95: clock automatically any longer. Grrr... */ cannam@95: cannam@95: return 0.01; cannam@95: } cannam@95: cannam@95: cannam@95: void timer_init(double tmin, int repeat) cannam@95: { cannam@95: static int inited = 0; cannam@95: cannam@95: if (inited) cannam@95: return; cannam@95: inited = 1; cannam@95: cannam@95: if (!repeat) cannam@95: repeat = 8; cannam@95: time_repeat = repeat; cannam@95: cannam@95: if (tmin > 0) cannam@95: time_min = tmin; cannam@95: else cannam@95: time_min = calibrate(); cannam@95: } cannam@95: cannam@95: static mytime t0[BENCH_NTIMERS]; cannam@95: cannam@95: void timer_start(int n) cannam@95: { cannam@95: BENCH_ASSERT(n >= 0 && n < BENCH_NTIMERS); cannam@95: t0[n] = get_time(); cannam@95: } cannam@95: cannam@95: double timer_stop(int n) cannam@95: { cannam@95: mytime t1; cannam@95: BENCH_ASSERT(n >= 0 && n < BENCH_NTIMERS); cannam@95: t1 = get_time(); cannam@95: return elapsed(t1, t0[n]); cannam@95: } cannam@95: