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