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