Skip to content

Commit bc31c31

Browse files
waywardmonkeyszayenz
authored andcommitted
Use std::chrono::steady_clock for timers.
This improves portability by not using `gettimeofday()` or `clock()`.
1 parent 78f6a02 commit bc31c31

File tree

5 files changed

+7
-69
lines changed

5 files changed

+7
-69
lines changed

CMakeLists.txt

-6
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,6 @@ endif ()
213213

214214
include(CheckSymbolExists)
215215
check_symbol_exists(mmap sys/mman.h HAVE_MMAP)
216-
check_symbol_exists(gettimeofday sys/time.h HAVE_GETTIMEOFDAY)
217-
if (HAVE_GETTIMEOFDAY)
218-
set(GECODE_USE_GETTIMEOFDAY 1)
219-
else ()
220-
set(GECODE_USE_CLOCK 1)
221-
endif ()
222216

223217
# Check for inline.
224218
include(CheckCSourceCompiles)

configure

-15
Original file line numberDiff line numberDiff line change
@@ -6537,21 +6537,6 @@ $as_echo "no" >&6; }
65376537

65386538

65396539

6540-
ac_fn_cxx_check_header_mongrel "$LINENO" "sys/time.h" "ac_cv_header_sys_time_h" "$ac_includes_default"
6541-
if test "x$ac_cv_header_sys_time_h" = xyes; then :
6542-
6543-
$as_echo "#define GECODE_USE_GETTIMEOFDAY 1" >>confdefs.h
6544-
6545-
else
6546-
6547-
$as_echo "#define GECODE_USE_CLOCK 1" >>confdefs.h
6548-
6549-
fi
6550-
6551-
6552-
6553-
6554-
65556540
# Check whether --with-freelist32-size-max was given.
65566541
if test "${with_freelist32_size_max+set}" = set; then :
65576542
withval=$with_freelist32_size_max;

gecode.m4

-6
Original file line numberDiff line numberDiff line change
@@ -1519,12 +1519,6 @@ AC_DEFUN([AC_GECODE_THREADS],[
15191519
fi
15201520
])
15211521

1522-
AC_DEFUN([AC_GECODE_TIMER],[
1523-
AC_CHECK_HEADER(sys/time.h,
1524-
[AC_DEFINE(GECODE_USE_GETTIMEOFDAY,1,[Use gettimeofday for time-measurement])],
1525-
[AC_DEFINE(GECODE_USE_CLOCK,1,[Use clock() for time-measurement])])
1526-
])
1527-
15281522
dnl check whether we have suifficiently recent versions of flex/bison
15291523
AC_DEFUN([AC_GECODE_FLEXBISON],
15301524
[AC_CHECK_TOOL(HAVEFLEX, flex)

gecode/support/config.hpp.in

-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@
114114
/* Whether to use unfair mutexes on macOS */
115115
#undef GECODE_USE_OSX_UNFAIR_MUTEX
116116

117-
/* Use clock() for time-measurement */
118-
#undef GECODE_USE_CLOCK
119-
120-
/* Use gettimeofday for time-measurement */
121-
#undef GECODE_USE_GETTIMEOFDAY
122-
123117
/* Gecode version */
124118
#undef GECODE_VERSION
125119

gecode/support/timer.hpp

+7-36
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@
3131
*
3232
*/
3333

34-
#ifdef GECODE_USE_GETTIMEOFDAY
35-
#include <sys/time.h>
36-
#endif
37-
38-
#ifdef GECODE_USE_CLOCK
39-
#include <ctime>
40-
#endif
34+
#include <chrono>
4135

4236
namespace Gecode { namespace Support {
4337

@@ -50,11 +44,9 @@ namespace Gecode { namespace Support {
5044
*/
5145
class GECODE_SUPPORT_EXPORT Timer {
5246
private:
53-
#if defined(GECODE_USE_GETTIMEOFDAY)
54-
timeval t0; ///< Start time
55-
#elif defined(GECODE_USE_CLOCK)
56-
clock_t t0; ///< Start time
57-
#endif
47+
using time_point = std::chrono::time_point<std::chrono::steady_clock>;
48+
time_point t0; ///< Start time
49+
5850
public:
5951
/// Start timer
6052
void start(void);
@@ -64,34 +56,13 @@ namespace Gecode { namespace Support {
6456

6557
inline void
6658
Timer::start(void) {
67-
#if defined(GECODE_USE_GETTIMEOFDAY)
68-
if (gettimeofday(&t0, nullptr))
69-
throw OperatingSystemError("Timer::start[gettimeofday]");
70-
#elif defined(GECODE_USE_CLOCK)
71-
t0 = clock();
72-
#endif
59+
t0 = std::chrono::steady_clock::now();
7360
}
7461

7562
inline double
7663
Timer::stop(void) {
77-
#if defined(GECODE_USE_GETTIMEOFDAY)
78-
timeval t1, t;
79-
if (gettimeofday(&t1, nullptr))
80-
throw OperatingSystemError("Timer::stop[gettimeofday]");
81-
82-
// t = t1 - t2
83-
t.tv_sec = t1.tv_sec - t0.tv_sec;
84-
t.tv_usec = t1.tv_usec - t0.tv_usec;
85-
if (t.tv_usec < 0) {
86-
t.tv_sec--;
87-
t.tv_usec += 1000000;
88-
}
89-
90-
return (static_cast<double>(t.tv_sec) * 1000.0) +
91-
(static_cast<double>(t.tv_usec)/1000.0);
92-
#elif defined(GECODE_USE_CLOCK)
93-
return (static_cast<double>(clock()-t0) / CLOCKS_PER_SEC) * 1000.0;
94-
#endif
64+
std::chrono::duration<double, std::milli> duration = std::chrono::steady_clock::now() - t0;
65+
return duration.count();
9566
}
9667

9768
}}

0 commit comments

Comments
 (0)