bench.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file bench.hh
  3. /// @brief Test the benchmarking microlibrary.
  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. #include <string>
  10. #include <limits>
  11. #include <chrono>
  12. #include <thread>
  13. #include <iomanip>
  14. #include <iostream>
  15. #include "ra/test.hh"
  16. #include "ra/bench.hh"
  17. using std::cout, std::endl, ra::TestRecorder;
  18. int main()
  19. {
  20. TestRecorder tr;
  21. tr.section("straight");
  22. {
  23. auto f = [](auto && a, auto && b)
  24. {
  25. std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  26. return a+b;
  27. };
  28. auto b = Benchmark {/* repeats */ 100, /* runs */ 10};
  29. auto vala = b.run(f, 1, 2);
  30. cout << "empty: " << (ra::size(vala)==0) << endl;
  31. b.report(std::cout, vala, 1e-9);
  32. auto valb = b.run(f, ra::iota(3), ra::iota(10, 3));
  33. b.report(std::cout, valb, 1e-9);
  34. }
  35. tr.section("fixture");
  36. {
  37. auto g = [](auto && repeat, auto && a, auto && b)
  38. {
  39. /* do stuff */
  40. repeat([&]() { std::this_thread::sleep_for(std::chrono::nanoseconds(1));
  41. return a+b; });
  42. /* do stuff */
  43. };
  44. auto b = Benchmark {/* repeats */ 100, /* runs */ 10};
  45. auto vala = b.run_f(g, 1, 2);
  46. cout << "empty: " << (ra::size(vala)==0) << endl;
  47. b.report(std::cout, vala, 1e-9);
  48. auto valb = b.run_f(g, ra::iota(3), ra::iota(10, 3));
  49. b.report(std::cout, valb, 1e-9);
  50. }
  51. return tr.summary();
  52. };