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