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