Skip to content

Commit

Permalink
Fix for bug in timing on Mac OS X
Browse files Browse the repository at this point in the history
Already fixed in the Solutions directory, copying across to here too.
  • Loading branch information
simonmcs committed Oct 19, 2013
1 parent e235cba commit d536e1a
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Exercises/Cpp_common/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,21 @@ class Timer
// WARNING: THIS IS PROBABLY BROKEN
struct timeval tv;
gettimeofday(&tv, 0);
ticks = (uint64_t) (tv.tv_sec - startTime_.tv_sec) * scale
+ (uint64_t) (tv.tv_usec - startTime_.tv_usec) * scale / (1000ULL * 1000ULL);
// check for overflow
if ((tv.tv_usec - startTime_.tv_usec) < 0)
{
// Remove a second from the second field and add it to the
// microseconds fields to prevent overflow.
// Then scale.
ticks = (uint64_t) (tv.tv_sec - startTime_.tv_sec - 1) * scale
+ (uint64_t) ((1000ULL * 1000ULL) + tv.tv_usec - startTime_.tv_usec)
* scale / (1000ULL * 1000ULL);
}
else
{
ticks = (uint64_t) (tv.tv_sec - startTime_.tv_sec) * scale
+ (uint64_t) (tv.tv_usec - startTime_.tv_usec) * scale / (1000ULL * 1000ULL);
}
#else
struct timespec tp;
::clock_gettime(CLOCK_MONOTONIC, &tp);
Expand Down

0 comments on commit d536e1a

Please sign in to comment.