From 49d1a5da1fe38a5bcb3ebe829d62b7eaa6d39636 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 30 Mar 2017 13:48:06 -0400 Subject: [PATCH] Adding better timer and testing --- include/CLI/Timer.hpp | 39 +++++++++++++++++++++++++++++++-------- tests/TimerTest.cpp | 13 +++++++++++-- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/include/CLI/Timer.hpp b/include/CLI/Timer.hpp index 48f3584..c7e9721 100644 --- a/include/CLI/Timer.hpp +++ b/include/CLI/Timer.hpp @@ -49,12 +49,33 @@ public: Timer(std::string title="Timer", time_print_t time_print = Simple) : 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) { + time_point start = start_; + double total_time; + + start_ = clock::now(); + size_t n = 0; + do { + f(); + std::chrono::duration elapsed = clock::now() - start_; + total_time = elapsed.count(); + } while (n++ < 100 && total_time < target_time); + + std::string out = make_time_str(total_time/n) + " for " + std::to_string(n) + " tries"; + start_ = start; + return out; + } /// This formats the numerical value for the time string + std::string make_time_str() const { time_point stop = clock::now(); - std::chrono::duration elapsed = stop - start_; + std::chrono::duration elapsed = stop - start_; double time = elapsed.count(); - + return make_time_str(time); + } + + std::string make_time_str(double time) const { auto print_it = [](double x, std::string unit){ char buffer[50]; std::snprintf(buffer, 50, "%.5g", x); @@ -62,14 +83,14 @@ public: }; // LCOV_EXCL_START - if(time < .001) - return print_it(time*1000000, "ns"); + if(time < .000001) + return print_it(time*1000000000, "ns"); + else if(time < .001) + return print_it(time*1000000, "us"); else if(time < 1) - return print_it(time*1000, "us"); - else if(time < 1000) - return print_it(time, "ms"); + return print_it(time*1000, "ms"); else - return print_it(time/1000, "s"); + return print_it(time, "s"); // LCOV_EXCL_END } @@ -79,6 +100,8 @@ public: } }; + + /// This class prints out the time upon destruction class AutoTimer : public Timer { public: diff --git a/tests/TimerTest.cpp b/tests/TimerTest.cpp index c9366ce..59648d6 100644 --- a/tests/TimerTest.cpp +++ b/tests/TimerTest.cpp @@ -25,10 +25,11 @@ TEST(Timer, STimes) { } */ -TEST(Timer, NStimes) { +TEST(Timer, UStimes) { CLI::Timer timer; + std::this_thread::sleep_for(std::chrono::microseconds(2)); std::string output = timer.to_string(); - EXPECT_THAT(output, HasSubstr(" ns")); + EXPECT_THAT(output, HasSubstr(" us")); } TEST(Timer, BigTimer) { @@ -51,3 +52,11 @@ TEST(Timer, PrintTimer) { std::string output = out.str(); EXPECT_THAT(output, HasSubstr("Timer")); } + +TEST(Timer, TimeItTimer) { + CLI::Timer timer; + std::string output = timer.time_it([](){ + std::this_thread::sleep_for(std::chrono::milliseconds(10));}, .1); + std::cout << output << std::endl; + EXPECT_THAT(output, HasSubstr("ms")); +} -- libgit2 0.21.4