diff --git a/include/CLI/Timer.hpp b/include/CLI/Timer.hpp index 158d3542..edb7eaa7 100644 --- a/include/CLI/Timer.hpp +++ b/include/CLI/Timer.hpp @@ -29,6 +29,9 @@ protected: /// This is the starting point (when the timer was created) time_point start_; + + /// This is the number of times cycles (print divides by this number) + size_t cycles {1}; public: @@ -50,7 +53,7 @@ public: : title_(title), time_print_(time_print), start_(clock::now()) {} /// Time a function by running it multiple times. Target time is the len to target. - inline std::string time_it(std::function f, double target_time=1) { + std::string time_it(std::function f, double target_time=1) { time_point start = start_; double total_time; @@ -98,6 +101,9 @@ public: std::string to_string() const { return time_print_(title_, make_time_str()); } + + /// Division sets the number of cycles + Timer& operator / (size_t val) {cycles = val; return *this;} }; diff --git a/tests/TimerTest.cpp b/tests/TimerTest.cpp index 67c70ef5..529ce09b 100644 --- a/tests/TimerTest.cpp +++ b/tests/TimerTest.cpp @@ -12,8 +12,10 @@ TEST(Timer, MSTimes) { CLI::Timer timer{"My Timer"}; std::this_thread::sleep_for(std::chrono::milliseconds(123)); std::string output = timer.to_string(); + std::new_output = (timer / 1000000).to_string(); EXPECT_THAT(output, HasSubstr("My Timer")); EXPECT_THAT(output, HasSubstr(" ms")); + EXPECT_THAT(new_output, HasSubstr(" ns")); } /* Takes too long