bench.hh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file bench.hh
  3. /// @brief Minimal benchmarking library.
  4. // (c) Daniel Llorens - 2017
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #pragma once
  10. #include <string>
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <chrono>
  14. #include "ra/operators.hh"
  15. #include "ra/io.hh"
  16. /*
  17. TODO
  18. - measure empty loops
  19. - better reporting
  20. - allow benchmarked functions to return results
  21. */
  22. struct Benchmark
  23. {
  24. constexpr static char const * esc_bold = "\x1b[01m";
  25. constexpr static char const * esc_unbold = "\x1b[0m";
  26. constexpr static char const * esc_red = "\x1b[31m";
  27. constexpr static char const * esc_green = "\x1b[32m";
  28. constexpr static char const * esc_cyan = "\x1b[36m";
  29. constexpr static char const * esc_yellow = "\x1b[33m";
  30. constexpr static char const * esc_blue = "\x1b[34m";
  31. constexpr static char const * esc_white = "\x1b[97m"; // an AIXTERM sequence
  32. constexpr static char const * esc_plain = "\x1b[39m";
  33. constexpr static char const * esc_reset = "\x1b[39m\x1b[0m"; // plain + unbold
  34. using clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
  35. std::chrono::high_resolution_clock,
  36. std::chrono::steady_clock>;
  37. static clock::duration
  38. lapse(clock::duration empty, clock::duration full)
  39. {
  40. // [ra08] count() to work around https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95242 on gcc 10.
  41. return (full.count()>empty.count()) ? full-empty : full;
  42. }
  43. static double
  44. toseconds(clock::duration const & t)
  45. {
  46. return std::chrono::duration<float, std::ratio<1, 1>>(t).count();
  47. }
  48. struct Value
  49. {
  50. std::string name;
  51. int repeats;
  52. clock::duration empty;
  53. ra::Big<clock::duration, 1> times;
  54. };
  55. static double avg(Value const & bv)
  56. {
  57. return toseconds(sum(bv.times))/bv.repeats/bv.times.size();
  58. }
  59. static double stddev(Value const & bv)
  60. {
  61. double m = avg(bv);
  62. return sqrt(sum(sqr(ra::map(toseconds, bv.times)/bv.repeats-m))/bv.times.size());
  63. }
  64. template <class B>
  65. void report(std::ostream & o, B const & b, double frac)
  66. {
  67. o << (info_str=="" ? "" : info_str + " : ") << ra::map([](auto && bv) { return avg(bv); }, b)/frac << std::endl;
  68. o << (info_str=="" ? "" : info_str + " : ") << ra::map([](auto && bv) { return stddev(bv); }, b)/frac << std::endl;
  69. info_str = "";
  70. }
  71. int const repeats_ = 1;
  72. int const runs_ = 1;
  73. std::string const name_ = "";
  74. std::string info_str = "";
  75. template <class ... A> Benchmark & info(A && ... a)
  76. {
  77. bool empty = (info_str=="");
  78. info_str += esc_plain;
  79. info_str += (empty ? "" : "; ");
  80. info_str += ra::format(a ...);
  81. info_str += esc_plain;
  82. return *this;
  83. }
  84. Benchmark name(std::string name_) { return Benchmark { repeats_, runs_, name_, "" }; }
  85. Benchmark repeats(int repeats_) { return Benchmark { repeats_, runs_, name_, "" }; }
  86. Benchmark runs(int runs_) { return Benchmark { repeats_, runs_, name_, "" }; }
  87. template <class F, class ... A> auto
  88. once(F && f, A && ... a)
  89. {
  90. auto t0 = clock::now();
  91. clock::duration empty = clock::now()-t0;
  92. ra::Big<clock::duration, 1> times;
  93. for (int k=0; k<runs_; ++k) {
  94. auto t0 = clock::now();
  95. for (int i=0; i<repeats_; ++i) {
  96. f(std::forward<A>(a) ...);
  97. }
  98. clock::duration full = clock::now()-t0;
  99. times.push_back(lapse(empty, full));
  100. }
  101. return Value { name_, repeats_, empty, std::move(times) };
  102. }
  103. template <class G, class ... A> auto
  104. once_f(G && g, A && ... a)
  105. {
  106. clock::duration empty;
  107. g([&](auto && f)
  108. {
  109. auto t0 = clock::now();
  110. empty = clock::now()-t0;
  111. }, std::forward<A>(a) ...);
  112. ra::Big<clock::duration, 1> times;
  113. for (int k=0; k<runs_; ++k) {
  114. g([&](auto && f)
  115. {
  116. auto t0 = clock::now();
  117. for (int i=0; i<repeats_; ++i) {
  118. f();
  119. }
  120. clock::duration full = clock::now()-t0;
  121. times.push_back(lapse(empty, full));
  122. }, std::forward<A>(a) ...);
  123. }
  124. return Value { name_, repeats_, empty, std::move(times) };
  125. }
  126. template <class F, class ... A> auto
  127. run(F && f, A && ... a)
  128. {
  129. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once(f, b ...); }, a ...));
  130. }
  131. template <class F, class ... A> auto
  132. run_f(F && f, A && ... a)
  133. {
  134. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once_f(f, b ...); }, a ...));
  135. }
  136. };
  137. namespace ra { template <> constexpr bool is_scalar_def<Benchmark::Value> = true; }