bench.H 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // (c) Daniel Llorens - 2017
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file bench.H
  7. /// @brief Minimal benchmarking library.
  8. #pragma once
  9. #include <string>
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <limits>
  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. using clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
  25. std::chrono::high_resolution_clock,
  26. std::chrono::steady_clock>;
  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 avg(Value const & bv)
  40. {
  41. return toseconds(sum(bv.times))/bv.repeats/bv.times.size();
  42. }
  43. static double stddev(Value const & bv)
  44. {
  45. double m = avg(bv);
  46. return sqrt(sum(sqr(map(toseconds, bv.times)/bv.repeats-m))/bv.times.size());
  47. }
  48. template <class B>
  49. static void report(std::ostream & o, B const & b, double frac)
  50. {
  51. o << map([](auto && bv) { return avg(bv); }, b)/frac << std::endl;
  52. o << map([](auto && bv) { return stddev(bv); }, b)/frac << std::endl;
  53. }
  54. int const repeats_ = 1;
  55. int const runs_ = 1;
  56. std::string const name_ = "";
  57. Benchmark name(std::string name_) { return Benchmark { repeats_, runs_, name_ }; }
  58. Benchmark repeats(int repeats_) { return Benchmark { repeats_, runs_, name_ }; }
  59. Benchmark runs(int runs_) { return Benchmark { repeats_, runs_, name_ }; }
  60. template <class F, class ... A> auto
  61. once(F && f, A && ... a)
  62. {
  63. auto t0 = clock::now();
  64. clock::duration empty = clock::now()-t0;
  65. ra::Big<clock::duration, 1> times;
  66. for (int k=0; k<runs_; ++k) {
  67. auto t0 = clock::now();
  68. for (int i=0; i<repeats_; ++i) {
  69. f(std::forward<A>(a) ...);
  70. }
  71. clock::duration full = clock::now()-t0;
  72. times.push_back(full>empty ? full-empty : full);
  73. }
  74. return Value { name_, repeats_, empty, std::move(times) };
  75. }
  76. template <class G, class ... A> auto
  77. once_f(G && g, A && ... a)
  78. {
  79. clock::duration empty;
  80. g([&](auto && f)
  81. {
  82. auto t0 = clock::now();
  83. empty = clock::now()-t0;
  84. }, std::forward<A>(a) ...);
  85. ra::Big<clock::duration, 1> times;
  86. for (int k=0; k<runs_; ++k) {
  87. g([&](auto && f)
  88. {
  89. auto t0 = clock::now();
  90. for (int i=0; i<repeats_; ++i) {
  91. f();
  92. }
  93. clock::duration full = clock::now()-t0;
  94. times.push_back(full>empty ? full-empty : full);
  95. }, std::forward<A>(a) ...);
  96. }
  97. return Value { name_, repeats_, empty, std::move(times) };
  98. }
  99. template <class F, class ... A> auto
  100. run(F && f, A && ... a)
  101. {
  102. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once(f, b ...); }, a ...));
  103. }
  104. template <class F, class ... A> auto
  105. run_f(F && f, A && ... a)
  106. {
  107. return ra::concrete(ra::from([this, &f](auto && ... b) { return this->once_f(f, b ...); }, a ...));
  108. }
  109. };
  110. namespace ra { template <> constexpr bool is_scalar_def<Benchmark::Value> = true; }