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