catch_timer.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_timer.hpp>
  7. #include <chrono>
  8. namespace Catch {
  9. namespace {
  10. static auto getCurrentNanosecondsSinceEpoch() -> uint64_t {
  11. return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
  12. }
  13. } // end unnamed namespace
  14. void Timer::start() {
  15. m_nanoseconds = getCurrentNanosecondsSinceEpoch();
  16. }
  17. auto Timer::getElapsedNanoseconds() const -> uint64_t {
  18. return getCurrentNanosecondsSinceEpoch() - m_nanoseconds;
  19. }
  20. auto Timer::getElapsedMicroseconds() const -> uint64_t {
  21. return getElapsedNanoseconds()/1000;
  22. }
  23. auto Timer::getElapsedMilliseconds() const -> unsigned int {
  24. return static_cast<unsigned int>(getElapsedMicroseconds()/1000);
  25. }
  26. auto Timer::getElapsedSeconds() const -> double {
  27. return getElapsedMicroseconds()/1000000.0;
  28. }
  29. } // namespace Catch