bench.cc 1.8 KB

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