Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #ifndef TESTER_H
  2. #define TESTER_H
  3.  
  4. // Examples:
  5. // tester<std::chrono::nanoseconds>::run_once(std::bind(&class::method, instance_ptr, std::placeholder::_1), 5);
  6. // tester<std::chrono::microseconds>::run_const_iter<10>(func, 6);
  7. // tester<std::chrono::nanoseconds>::run_dyn_iter(1000000, func, 6);
  8.  
  9.  
  10. #include <chrono>
  11.  
  12.  
  13. template<size_t count>
  14. class run_loop {
  15. public:
  16. template<typename Callable, typename ...Args>
  17. static inline void run(Callable&& func, Args&&... args) {
  18. volatile auto result = std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
  19. (void)result;
  20. run_loop<count - 1>::run(func, args...);
  21. }
  22. };
  23.  
  24. template<>
  25. class run_loop<1> {
  26. public:
  27. template<typename Callable, typename ...Args>
  28. static inline void run(Callable&& func, Args&&... args) {
  29. volatile auto result = std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
  30. (void)result;
  31. }
  32. };
  33.  
  34.  
  35. template<typename Time_t = std::chrono::milliseconds, typename Clock_t = std::chrono::high_resolution_clock>
  36. class tester {
  37. public:
  38. template<typename Callable, typename ...Args>
  39. static typename Time_t::rep run_once(Callable&& func, Args&&... args) {
  40. return run_const_iter<1>(func, args...);
  41. }
  42.  
  43. template<typename Callable, typename ...Args>
  44. static typename Time_t::rep run_dyn_iter(size_t iterations, Callable&& func, Args&&... args) {
  45. auto start = Clock_t::now();
  46. for (size_t i = 0; i < iterations; ++i) {
  47. run_loop<1>::run(func, args...);
  48. }
  49. auto duration = std::chrono::duration_cast<Time_t>(Clock_t::now() - start);
  50. return duration.count();
  51. }
  52.  
  53. template<size_t iters, typename Callable, typename ...Args>
  54. static typename Time_t::rep run_const_iter(Callable&& func, Args&&... args) {
  55. auto start = Clock_t::now();
  56. run_loop<iters>::run(func, args...);
  57. auto duration = std::chrono::duration_cast<Time_t>(Clock_t::now() - start);
  58. return duration.count();
  59. }
  60. };
  61.  
  62.  
  63. #endif // TESTER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement