benchmark.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // Copyright (c) 2018 Stefan Seefeld
  3. // All rights reserved.
  4. //
  5. // This file is part of Boost.uBLAS. It is made available under the
  6. // Boost Software License, Version 1.0.
  7. // (Consult LICENSE or http://www.boost.org/LICENSE_1_0.txt)
  8. #pragma once
  9. #include <iostream>
  10. #include <chrono>
  11. #include <ctime>
  12. #include <cmath>
  13. #include <string>
  14. #include <vector>
  15. namespace boost { namespace numeric { namespace ublas { namespace benchmark {
  16. class benchmark
  17. {
  18. using clock = std::chrono::system_clock;
  19. public:
  20. benchmark(std::string const &name) : name_(name) {}
  21. void print_header()
  22. {
  23. std::cout << "# benchmark : " << name_ << '\n'
  24. << "# size \ttime (ms)" << std::endl;
  25. }
  26. virtual void setup(long) {}
  27. virtual void operation(long) {}
  28. virtual void teardown() {}
  29. void run(std::vector<long> const &sizes, unsigned times = 10)
  30. {
  31. print_header();
  32. for (auto s : sizes)
  33. {
  34. setup(s);
  35. auto start = clock::now();
  36. for (unsigned i = 0; i != times; ++i)
  37. operation(s);
  38. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - start);
  39. teardown();
  40. std::cout << s << '\t' << duration.count()*1./times << std::endl;
  41. }
  42. }
  43. private:
  44. std::string name_;
  45. };
  46. }}}}