Commit 416e6a04430f19defb40f68d3ae8387da0894650
1 parent
4755164f
Adding division to timer
Showing
2 changed files
with
9 additions
and
1 deletions
include/CLI/Timer.hpp
| ... | ... | @@ -29,6 +29,9 @@ protected: |
| 29 | 29 | |
| 30 | 30 | /// This is the starting point (when the timer was created) |
| 31 | 31 | time_point start_; |
| 32 | + | |
| 33 | + /// This is the number of times cycles (print divides by this number) | |
| 34 | + size_t cycles {1}; | |
| 32 | 35 | |
| 33 | 36 | public: |
| 34 | 37 | |
| ... | ... | @@ -50,7 +53,7 @@ public: |
| 50 | 53 | : title_(title), time_print_(time_print), start_(clock::now()) {} |
| 51 | 54 | |
| 52 | 55 | /// Time a function by running it multiple times. Target time is the len to target. |
| 53 | - inline std::string time_it(std::function<void()> f, double target_time=1) { | |
| 56 | + std::string time_it(std::function<void()> f, double target_time=1) { | |
| 54 | 57 | time_point start = start_; |
| 55 | 58 | double total_time; |
| 56 | 59 | |
| ... | ... | @@ -98,6 +101,9 @@ public: |
| 98 | 101 | std::string to_string() const { |
| 99 | 102 | return time_print_(title_, make_time_str()); |
| 100 | 103 | } |
| 104 | + | |
| 105 | + /// Division sets the number of cycles | |
| 106 | + Timer& operator / (size_t val) {cycles = val; return *this;} | |
| 101 | 107 | }; |
| 102 | 108 | |
| 103 | 109 | ... | ... |
tests/TimerTest.cpp
| ... | ... | @@ -12,8 +12,10 @@ TEST(Timer, MSTimes) { |
| 12 | 12 | CLI::Timer timer{"My Timer"}; |
| 13 | 13 | std::this_thread::sleep_for(std::chrono::milliseconds(123)); |
| 14 | 14 | std::string output = timer.to_string(); |
| 15 | + std::new_output = (timer / 1000000).to_string(); | |
| 15 | 16 | EXPECT_THAT(output, HasSubstr("My Timer")); |
| 16 | 17 | EXPECT_THAT(output, HasSubstr(" ms")); |
| 18 | + EXPECT_THAT(new_output, HasSubstr(" ns")); | |
| 17 | 19 | } |
| 18 | 20 | |
| 19 | 21 | /* Takes too long | ... | ... |