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