Skip to content

Commit

Permalink
Use std::chrono::steady_clock for timers.
Browse files Browse the repository at this point in the history
This improves portability by not using `gettimeofday()` or `clock()`.
  • Loading branch information
waywardmonkeys authored and zayenz committed Dec 5, 2023
1 parent 78f6a02 commit bc31c31
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 69 deletions.
6 changes: 0 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 0 additions & 15 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions gecode.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions gecode/support/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 7 additions & 36 deletions gecode/support/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@
*
*/

#ifdef GECODE_USE_GETTIMEOFDAY
#include <sys/time.h>
#endif

#ifdef GECODE_USE_CLOCK
#include <ctime>
#endif
#include <chrono>

namespace Gecode { namespace Support {

Expand All @@ -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<std::chrono::steady_clock>;
time_point t0; ///< Start time

public:
/// Start timer
void start(void);
Expand All @@ -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<double>(t.tv_sec) * 1000.0) +
(static_cast<double>(t.tv_usec)/1000.0);
#elif defined(GECODE_USE_CLOCK)
return (static_cast<double>(clock()-t0) / CLOCKS_PER_SEC) * 1000.0;
#endif
std::chrono::duration<double, std::milli> duration = std::chrono::steady_clock::now() - t0;
return duration.count();
}

}}
Expand Down

0 comments on commit bc31c31

Please sign in to comment.