bench.cc 1.8 KB

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