bench.H 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file bench.H
  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.H"
  15. #include "ra/io.H"
  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 double
  38. toseconds(clock::duration const & t)
  39. {
  40. return std::chrono::duration<float, std::ratio<1, 1>>(t).count();
  41. }
  42. struct Value
  43. {
  44. std::string name;
  45. int repeats;
  46. clock::duration empty;
  47. ra::Big<clock::duration, 1> times;
  48. };
  49. static double avg(Value const & bv)
  50. {
  51. return toseconds(sum(bv.times))/bv.repeats/bv.times.size();
  52. }
  53. static double stddev(Value const & bv)
  54. {
  55. double m = avg(bv);
  56. return sqrt(sum(sqr(ra::map(toseconds, bv.times)/bv.repeats-m))/bv.times.size());
  57. }
  58. template <class B>
  59. void report(std::ostream & o, B const & b, double frac)
  60. {
  61. o << (info_str=="" ? "" : info_str + " : ") << ra::map([](auto && bv) { return avg(bv); }, b)/frac << std::endl;
  62. o << (info_str=="" ? "" : info_str + " : ") << ra::map([](auto && bv) { return stddev(bv); }, b)/frac << std::endl;
  63. info_str = "";
  64. }
  65. int const repeats_ = 1;
  66. int const runs_ = 1;
  67. std::string const name_ = "";
  68. std::string info_str = "";
  69. template <class ... A> Benchmark & info(A && ... a)
  70. {
  71. bool empty = (info_str=="");
  72. info_str += esc_plain;
  73. info_str += (empty ? "" : "; ");
  74. info_str += ra::format(a ...);
  75. info_str += esc_plain;
  76. return *this;
  77. }
  78. Benchmark name(std::string name_) { return Benchmark { repeats_, runs_, name_, "" }; }
  79. Benchmark repeats(int repeats_) { return Benchmark { repeats_, runs_, name_, "" }; }
  80. Benchmark runs(int runs_) { return Benchmark { repeats_, runs_, name_, "" }; }
  81. template <class F, class ... A> auto
  82. once(F && f, A && ... a)
  83. {
  84. auto t0 = clock::now();
  85. clock::duration empty = clock::now()-t0;
  86. ra::Big<clock::duration, 1> times;
  87. for (int k=0; k<runs_; ++k) {
  88. auto t0 = clock::now();
  89. for (int i=0; i<repeats_; ++i) {
  90. f(std::forward<A>(a) ...);
  91. }
  92. clock::duration full = clock::now()-t0;
  93. times.push_back(full>empty ? full-empty : full);
  94. }
  95. return Value { name_, repeats_, empty, std::move(times) };
  96. }
  97. template <class G, class ... A> auto
  98. once_f(G && g, A && ... a)
  99. {
  100. clock::duration empty;
  101. g([&](auto && f)
  102. {
  103. auto t0 = clock::now();
  104. empty = clock::now()-t0;
  105. }, std::forward<A>(a) ...);
  106. ra::Big<clock::duration, 1> times;
  107. for (int k=0; k<runs_; ++k) {
  108. g([&](auto && f)
  109. {
  110. auto t0 = clock::now();
  111. for (int i=0; i<repeats_; ++i) {
  112. f();
  113. }
  114. clock::duration full = clock::now()-t0;
  115. times.push_back(full>empty ? full-empty : full);
  116. }, std::forward<A>(a) ...);
  117. }
  118. return Value { name_, repeats_, empty, std::move(times) };
  119. }
  120. template <class F, class ... A> auto
  121. run(F && f, A && ... a)
  122. {
  123. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once(f, b ...); }, a ...));
  124. }
  125. template <class F, class ... A> auto
  126. run_f(F && f, A && ... a)
  127. {
  128. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once_f(f, b ...); }, a ...));
  129. }
  130. };
  131. namespace ra { template <> constexpr bool is_scalar_def<Benchmark::Value> = true; }