Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #ifndef BENCHMARK_HPP
  2. #define BENCHMARK_HPP
  3. /*!
  4. * \file benchmark.hpp
  5. *
  6. * Benchmark for functions in C++. Use it only in release mode.
  7. */
  8.  
  9. #include <iostream>
  10. #include <functional>
  11. #include <chrono>
  12. #include <string>
  13.  
  14. /*!
  15. * \brief Benchmarks the run speed of a function.
  16. * \param message The message to print
  17. * \param func The function to be evaluated. This function have zero arguments.
  18. * \param compute_times Times to compute the function
  19. *
  20. * Runs the function func compute_times times and use the average time as result.
  21. */
  22. template <typename F>
  23. void evaluate_speed(const std::string &message,
  24. F func,
  25. int compute_times = 100) {
  26. auto start = std::chrono::high_resolution_clock::now();
  27. for (auto i = 0; i != compute_times; ++i) {
  28. func();
  29. }
  30. auto end = std::chrono::high_resolution_clock::now();
  31.  
  32. auto total_time = std::chrono::duration_cast<std::chrono::milliseconds>
  33. (end - start).count();
  34.  
  35. auto average_time = static_cast<double>(total_time) / compute_times;
  36. std::cout << message << ": " << average_time << "ms\n";
  37. }
  38.  
  39.  
  40. #endif // BENCHMARK_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement