Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TESTER_H
- #define TESTER_H
- // Examples:
- // tester<std::chrono::nanoseconds>::run_once(std::bind(&class::method, instance_ptr, std::placeholder::_1), 5);
- // tester<std::chrono::microseconds>::run_const_iter<10>(func, 6);
- // tester<std::chrono::nanoseconds>::run_dyn_iter(1000000, func, 6);
- #include <chrono>
- template<size_t count>
- class run_loop {
- public:
- template<typename Callable, typename ...Args>
- static inline void run(Callable&& func, Args&&... args) {
- volatile auto result = std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
- (void)result;
- run_loop<count - 1>::run(func, args...);
- }
- };
- template<>
- class run_loop<1> {
- public:
- template<typename Callable, typename ...Args>
- static inline void run(Callable&& func, Args&&... args) {
- volatile auto result = std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
- (void)result;
- }
- };
- template<typename Time_t = std::chrono::milliseconds, typename Clock_t = std::chrono::high_resolution_clock>
- class tester {
- public:
- template<typename Callable, typename ...Args>
- static typename Time_t::rep run_once(Callable&& func, Args&&... args) {
- return run_const_iter<1>(func, args...);
- }
- template<typename Callable, typename ...Args>
- static typename Time_t::rep run_dyn_iter(size_t iterations, Callable&& func, Args&&... args) {
- auto start = Clock_t::now();
- for (size_t i = 0; i < iterations; ++i) {
- run_loop<1>::run(func, args...);
- }
- auto duration = std::chrono::duration_cast<Time_t>(Clock_t::now() - start);
- return duration.count();
- }
- template<size_t iters, typename Callable, typename ...Args>
- static typename Time_t::rep run_const_iter(Callable&& func, Args&&... args) {
- auto start = Clock_t::now();
- run_loop<iters>::run(func, args...);
- auto duration = std::chrono::duration_cast<Time_t>(Clock_t::now() - start);
- return duration.count();
- }
- };
- #endif // TESTER_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement