Commit 49d1a5da1fe38a5bcb3ebe829d62b7eaa6d39636

Authored by Henry Schreiner
1 parent ab29808a

Adding better timer and testing

include/CLI/Timer.hpp
@@ -49,12 +49,33 @@ public: @@ -49,12 +49,33 @@ public:
49 Timer(std::string title="Timer", time_print_t time_print = Simple) 49 Timer(std::string title="Timer", time_print_t time_print = Simple)
50 : title_(title), time_print_(time_print), start_(clock::now()) {} 50 : title_(title), time_print_(time_print), start_(clock::now()) {}
51 51
  52 + /// 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) {
  54 + time_point start = start_;
  55 + double total_time;
  56 +
  57 + start_ = clock::now();
  58 + size_t n = 0;
  59 + do {
  60 + f();
  61 + std::chrono::duration<double> elapsed = clock::now() - start_;
  62 + total_time = elapsed.count();
  63 + } while (n++ < 100 && total_time < target_time);
  64 +
  65 + std::string out = make_time_str(total_time/n) + " for " + std::to_string(n) + " tries";
  66 + start_ = start;
  67 + return out;
  68 + }
52 /// This formats the numerical value for the time string 69 /// This formats the numerical value for the time string
  70 +
53 std::string make_time_str() const { 71 std::string make_time_str() const {
54 time_point stop = clock::now(); 72 time_point stop = clock::now();
55 - std::chrono::duration<double, std::milli> elapsed = stop - start_; 73 + std::chrono::duration<double> elapsed = stop - start_;
56 double time = elapsed.count(); 74 double time = elapsed.count();
57 - 75 + return make_time_str(time);
  76 + }
  77 +
  78 + std::string make_time_str(double time) const {
58 auto print_it = [](double x, std::string unit){ 79 auto print_it = [](double x, std::string unit){
59 char buffer[50]; 80 char buffer[50];
60 std::snprintf(buffer, 50, "%.5g", x); 81 std::snprintf(buffer, 50, "%.5g", x);
@@ -62,14 +83,14 @@ public: @@ -62,14 +83,14 @@ public:
62 }; 83 };
63 84
64 // LCOV_EXCL_START 85 // LCOV_EXCL_START
65 - if(time < .001)  
66 - return print_it(time*1000000, "ns"); 86 + if(time < .000001)
  87 + return print_it(time*1000000000, "ns");
  88 + else if(time < .001)
  89 + return print_it(time*1000000, "us");
67 else if(time < 1) 90 else if(time < 1)
68 - return print_it(time*1000, "us");  
69 - else if(time < 1000)  
70 - return print_it(time, "ms"); 91 + return print_it(time*1000, "ms");
71 else 92 else
72 - return print_it(time/1000, "s"); 93 + return print_it(time, "s");
73 // LCOV_EXCL_END 94 // LCOV_EXCL_END
74 } 95 }
75 96
@@ -79,6 +100,8 @@ public: @@ -79,6 +100,8 @@ public:
79 } 100 }
80 }; 101 };
81 102
  103 +
  104 +
82 /// This class prints out the time upon destruction 105 /// This class prints out the time upon destruction
83 class AutoTimer : public Timer { 106 class AutoTimer : public Timer {
84 public: 107 public:
tests/TimerTest.cpp
@@ -25,10 +25,11 @@ TEST(Timer, STimes) { @@ -25,10 +25,11 @@ TEST(Timer, STimes) {
25 } 25 }
26 */ 26 */
27 27
28 -TEST(Timer, NStimes) { 28 +TEST(Timer, UStimes) {
29 CLI::Timer timer; 29 CLI::Timer timer;
  30 + std::this_thread::sleep_for(std::chrono::microseconds(2));
30 std::string output = timer.to_string(); 31 std::string output = timer.to_string();
31 - EXPECT_THAT(output, HasSubstr(" ns")); 32 + EXPECT_THAT(output, HasSubstr(" us"));
32 } 33 }
33 34
34 TEST(Timer, BigTimer) { 35 TEST(Timer, BigTimer) {
@@ -51,3 +52,11 @@ TEST(Timer, PrintTimer) { @@ -51,3 +52,11 @@ TEST(Timer, PrintTimer) {
51 std::string output = out.str(); 52 std::string output = out.str();
52 EXPECT_THAT(output, HasSubstr("Timer")); 53 EXPECT_THAT(output, HasSubstr("Timer"));
53 } 54 }
  55 +
  56 +TEST(Timer, TimeItTimer) {
  57 + CLI::Timer timer;
  58 + std::string output = timer.time_it([](){
  59 + std::this_thread::sleep_for(std::chrono::milliseconds(10));}, .1);
  60 + std::cout << output << std::endl;
  61 + EXPECT_THAT(output, HasSubstr("ms"));
  62 +}