bench.cc 1.8 KB

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