bench.hh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra - Benchmarking library.
  3. // (c) Daniel Llorens - 2017-2023
  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 "test.hh"
  14. // TODO measure empty loops
  15. // TODO better reporting
  16. // TODO allow benchmarked functions to return results
  17. struct Benchmark
  18. {
  19. using clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
  20. std::chrono::high_resolution_clock,
  21. std::chrono::steady_clock>;
  22. static clock::duration
  23. lapse(clock::duration empty, clock::duration full)
  24. {
  25. return (full>empty) ? full-empty : full;
  26. }
  27. static double
  28. toseconds(clock::duration const & t)
  29. {
  30. return std::chrono::duration<float, std::ratio<1, 1>>(t).count();
  31. }
  32. struct Value
  33. {
  34. std::string name;
  35. int repeats;
  36. clock::duration empty;
  37. ra::Big<clock::duration, 1> times;
  38. };
  39. static double
  40. avg(Value const & bv)
  41. {
  42. return toseconds(sum(bv.times))/bv.repeats/bv.times.size();
  43. }
  44. static double
  45. stddev(Value const & bv)
  46. {
  47. double m = avg(bv);
  48. return sqrt(sum(sqr(ra::map(toseconds, bv.times)/bv.repeats-m))/bv.times.size());
  49. }
  50. void
  51. report(std::ostream & o, auto const & b, double frac)
  52. {
  53. o << (info_str=="" ? "" : info_str + " : ") << ra::map([](auto && bv) { return avg(bv); }, b)/frac << std::endl;
  54. o << (info_str=="" ? "" : info_str + " : ") << ra::map([](auto && bv) { return stddev(bv); }, b)/frac << std::endl;
  55. info_str = "";
  56. }
  57. int const repeats_ = 1;
  58. int const runs_ = 1;
  59. std::string const name_ = "";
  60. std::string info_str = "";
  61. Benchmark &
  62. info(auto && ... a)
  63. {
  64. bool empty = (info_str=="");
  65. info_str += ra::esc::plain;
  66. info_str += (empty ? "" : "; ");
  67. info_str += ra::format(a ...);
  68. info_str += ra::esc::plain;
  69. return *this;
  70. }
  71. Benchmark name(std::string name_) { return Benchmark { repeats_, runs_, name_, "" }; }
  72. Benchmark repeats(int repeats_) { return Benchmark { repeats_, runs_, name_, "" }; }
  73. Benchmark runs(int runs_) { return Benchmark { repeats_, runs_, name_, "" }; }
  74. auto
  75. once(auto && f, auto && ... a)
  76. {
  77. auto t0 = clock::now();
  78. clock::duration empty = clock::now()-t0;
  79. ra::Big<clock::duration, 1> times;
  80. for (int k=0; k<runs_; ++k) {
  81. auto t0 = clock::now();
  82. for (int i=0; i<repeats_; ++i) {
  83. f(RA_FWD(a) ...);
  84. }
  85. clock::duration full = clock::now()-t0;
  86. times.push_back(lapse(empty, full));
  87. }
  88. return Value { name_, repeats_, empty, std::move(times) };
  89. }
  90. auto
  91. once_f(auto && g, auto && ... a)
  92. {
  93. clock::duration empty;
  94. g([&](auto && f)
  95. {
  96. auto t0 = clock::now();
  97. empty = clock::now()-t0;
  98. }, RA_FWD(a) ...);
  99. ra::Big<clock::duration, 1> times;
  100. for (int k=0; k<runs_; ++k) {
  101. g([&](auto && f)
  102. {
  103. auto t0 = clock::now();
  104. for (int i=0; i<repeats_; ++i) {
  105. f();
  106. }
  107. clock::duration full = clock::now()-t0;
  108. times.push_back(lapse(empty, full));
  109. }, RA_FWD(a) ...);
  110. }
  111. return Value { name_, repeats_, empty, std::move(times) };
  112. }
  113. auto
  114. run(auto && f, auto && ... a)
  115. {
  116. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once(f, b ...); }, a ...));
  117. }
  118. auto
  119. run_f(auto && f, auto && ... a)
  120. {
  121. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once_f(f, b ...); }, a ...));
  122. }
  123. };
  124. template <> constexpr bool ra::is_scalar_def<Benchmark::Value> = true;