catch_timer.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Created by Phil on 05/08/2013.
  3. * Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include "catch_timer.h"
  9. #include <chrono>
  10. static const uint64_t nanosecondsInSecond = 1000000000;
  11. namespace Catch {
  12. auto getCurrentNanosecondsSinceEpoch() -> uint64_t {
  13. return std::chrono::duration_cast<std::chrono::nanoseconds>( std::chrono::high_resolution_clock::now().time_since_epoch() ).count();
  14. }
  15. namespace {
  16. auto estimateClockResolution() -> uint64_t {
  17. uint64_t sum = 0;
  18. static const uint64_t iterations = 1000000;
  19. auto startTime = getCurrentNanosecondsSinceEpoch();
  20. for( std::size_t i = 0; i < iterations; ++i ) {
  21. uint64_t ticks;
  22. uint64_t baseTicks = getCurrentNanosecondsSinceEpoch();
  23. do {
  24. ticks = getCurrentNanosecondsSinceEpoch();
  25. } while( ticks == baseTicks );
  26. auto delta = ticks - baseTicks;
  27. sum += delta;
  28. // If we have been calibrating for over 3 seconds -- the clock
  29. // is terrible and we should move on.
  30. // TBD: How to signal that the measured resolution is probably wrong?
  31. if (ticks > startTime + 3 * nanosecondsInSecond) {
  32. return sum / ( i + 1u );
  33. }
  34. }
  35. // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers
  36. // - and potentially do more iterations if there's a high variance.
  37. return sum/iterations;
  38. }
  39. }
  40. auto getEstimatedClockResolution() -> uint64_t {
  41. static auto s_resolution = estimateClockResolution();
  42. return s_resolution;
  43. }
  44. void Timer::start() {
  45. m_nanoseconds = getCurrentNanosecondsSinceEpoch();
  46. }
  47. auto Timer::getElapsedNanoseconds() const -> uint64_t {
  48. return getCurrentNanosecondsSinceEpoch() - m_nanoseconds;
  49. }
  50. auto Timer::getElapsedMicroseconds() const -> uint64_t {
  51. return getElapsedNanoseconds()/1000;
  52. }
  53. auto Timer::getElapsedMilliseconds() const -> unsigned int {
  54. return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
  55. }
  56. auto Timer::getElapsedSeconds() const -> double {
  57. return getElapsedMicroseconds()/1000000.0;
  58. }
  59. } // namespace Catch