bench.C 1.8 KB

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