1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-01 05:03:52 +00:00

Adding division to timer

This commit is contained in:
Henry Fredrick Schreiner 2017-05-15 11:10:55 -04:00
parent 4755164f39
commit 416e6a0443
2 changed files with 9 additions and 1 deletions

View File

@ -30,6 +30,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:
/// Standard print function, this one is set by default
@ -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<void()> f, double target_time=1) {
std::string time_it(std::function<void()> 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;}
};

View File

@ -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