bench.hh 4.9 KB

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