• Docs >
  • Program Listing for File timer.hpp
Shortcuts

Program Listing for File timer.hpp

Return to documentation for file (include/ripple/utility/timer.hpp)

#ifndef RIPPLE_UTILITY_TIMER_HPP
#define RIPPLE_UTILITY_TIMER_HPP

#include <chrono>

namespace ripple {

struct Timer {
  // clang-format off
  using Clock     = std::chrono::high_resolution_clock;
  using TimePoint = typename Clock::time_point;
  using Duration  = std::chrono::duration<double>;
  // clang-format on

  static constexpr double microsecs_to_millisecs = 1000.0;

  Timer() noexcept {
    reset();
  }

  auto reset() noexcept -> void {
    start_ = Clock::now();
  }

  auto elapsed(bool reset_timer = false) noexcept -> double {
    TimePoint curr      = Clock::now();
    Duration  time_span = std::chrono::duration_cast<Duration>(curr - start_);

    if (reset_timer) {
      reset();
    }
    return time_span.count();
  }

  auto elapsed_msec(bool reset_timer = false) noexcept -> double {
    return elapsed(reset_timer) * microsecs_to_millisecs;
  }

 private:
  TimePoint start_;
};

} // namespace ripple

#endif // RIPPLE_UTILITY_TIMER_HPP

Docs

Access comprehensive developer documentation for Ripple

View Docs

Tutorials

Get tutorials to help with understand all features

View Tutorials

Examples

Find examples to help get started

View Examples