From bc31c318b73b47a085551fc3a6fa70dba89dad28 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sat, 2 Dec 2023 21:14:02 +0700 Subject: [PATCH] Use std::chrono::steady_clock for timers. This improves portability by not using `gettimeofday()` or `clock()`. --- CMakeLists.txt | 6 ----- configure | 15 ------------- gecode.m4 | 6 ----- gecode/support/config.hpp.in | 6 ----- gecode/support/timer.hpp | 43 ++++++------------------------------ 5 files changed, 7 insertions(+), 69 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8980604d52..6b4551ccc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -213,12 +213,6 @@ endif () include(CheckSymbolExists) check_symbol_exists(mmap sys/mman.h HAVE_MMAP) -check_symbol_exists(gettimeofday sys/time.h HAVE_GETTIMEOFDAY) -if (HAVE_GETTIMEOFDAY) - set(GECODE_USE_GETTIMEOFDAY 1) -else () - set(GECODE_USE_CLOCK 1) -endif () # Check for inline. include(CheckCSourceCompiles) diff --git a/configure b/configure index 71aaaae472..960c2d0770 100755 --- a/configure +++ b/configure @@ -6537,21 +6537,6 @@ $as_echo "no" >&6; } - ac_fn_cxx_check_header_mongrel "$LINENO" "sys/time.h" "ac_cv_header_sys_time_h" "$ac_includes_default" -if test "x$ac_cv_header_sys_time_h" = xyes; then : - -$as_echo "#define GECODE_USE_GETTIMEOFDAY 1" >>confdefs.h - -else - -$as_echo "#define GECODE_USE_CLOCK 1" >>confdefs.h - -fi - - - - - # Check whether --with-freelist32-size-max was given. if test "${with_freelist32_size_max+set}" = set; then : withval=$with_freelist32_size_max; diff --git a/gecode.m4 b/gecode.m4 index dc31ee8d5b..68cf637f49 100755 --- a/gecode.m4 +++ b/gecode.m4 @@ -1519,12 +1519,6 @@ AC_DEFUN([AC_GECODE_THREADS],[ fi ]) -AC_DEFUN([AC_GECODE_TIMER],[ - AC_CHECK_HEADER(sys/time.h, - [AC_DEFINE(GECODE_USE_GETTIMEOFDAY,1,[Use gettimeofday for time-measurement])], - [AC_DEFINE(GECODE_USE_CLOCK,1,[Use clock() for time-measurement])]) -]) - dnl check whether we have suifficiently recent versions of flex/bison AC_DEFUN([AC_GECODE_FLEXBISON], [AC_CHECK_TOOL(HAVEFLEX, flex) diff --git a/gecode/support/config.hpp.in b/gecode/support/config.hpp.in index 88a8e020bd..4ccee0f761 100644 --- a/gecode/support/config.hpp.in +++ b/gecode/support/config.hpp.in @@ -114,12 +114,6 @@ /* Whether to use unfair mutexes on macOS */ #undef GECODE_USE_OSX_UNFAIR_MUTEX -/* Use clock() for time-measurement */ -#undef GECODE_USE_CLOCK - -/* Use gettimeofday for time-measurement */ -#undef GECODE_USE_GETTIMEOFDAY - /* Gecode version */ #undef GECODE_VERSION diff --git a/gecode/support/timer.hpp b/gecode/support/timer.hpp index cc1e224601..85f9f91b40 100644 --- a/gecode/support/timer.hpp +++ b/gecode/support/timer.hpp @@ -31,13 +31,7 @@ * */ -#ifdef GECODE_USE_GETTIMEOFDAY -#include -#endif - -#ifdef GECODE_USE_CLOCK -#include -#endif +#include namespace Gecode { namespace Support { @@ -50,11 +44,9 @@ namespace Gecode { namespace Support { */ class GECODE_SUPPORT_EXPORT Timer { private: -#if defined(GECODE_USE_GETTIMEOFDAY) - timeval t0; ///< Start time -#elif defined(GECODE_USE_CLOCK) - clock_t t0; ///< Start time -#endif + using time_point = std::chrono::time_point; + time_point t0; ///< Start time + public: /// Start timer void start(void); @@ -64,34 +56,13 @@ namespace Gecode { namespace Support { inline void Timer::start(void) { -#if defined(GECODE_USE_GETTIMEOFDAY) - if (gettimeofday(&t0, nullptr)) - throw OperatingSystemError("Timer::start[gettimeofday]"); -#elif defined(GECODE_USE_CLOCK) - t0 = clock(); -#endif + t0 = std::chrono::steady_clock::now(); } inline double Timer::stop(void) { -#if defined(GECODE_USE_GETTIMEOFDAY) - timeval t1, t; - if (gettimeofday(&t1, nullptr)) - throw OperatingSystemError("Timer::stop[gettimeofday]"); - - // t = t1 - t2 - t.tv_sec = t1.tv_sec - t0.tv_sec; - t.tv_usec = t1.tv_usec - t0.tv_usec; - if (t.tv_usec < 0) { - t.tv_sec--; - t.tv_usec += 1000000; - } - - return (static_cast(t.tv_sec) * 1000.0) + - (static_cast(t.tv_usec)/1000.0); -#elif defined(GECODE_USE_CLOCK) - return (static_cast(clock()-t0) / CLOCKS_PER_SEC) * 1000.0; -#endif + std::chrono::duration duration = std::chrono::steady_clock::now() - t0; + return duration.count(); } }}