Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef BENCHMARK_HPP
- #define BENCHMARK_HPP
- /*!
- * \file benchmark.hpp
- *
- * Benchmark for functions in C++. Use it only in release mode.
- */
- #include <iostream>
- #include <functional>
- #include <chrono>
- #include <string>
- /*!
- * \brief Benchmarks the run speed of a function.
- * \param message The message to print
- * \param func The function to be evaluated. This function have zero arguments.
- * \param compute_times Times to compute the function
- *
- * Runs the function func compute_times times and use the average time as result.
- */
- template <typename F>
- void evaluate_speed(const std::string &message,
- F func,
- int compute_times = 100) {
- auto start = std::chrono::high_resolution_clock::now();
- for (auto i = 0; i != compute_times; ++i) {
- func();
- }
- auto end = std::chrono::high_resolution_clock::now();
- auto total_time = std::chrono::duration_cast<std::chrono::milliseconds>
- (end - start).count();
- auto average_time = static_cast<double>(total_time) / compute_times;
- std::cout << message << ": " << average_time << "ms\n";
- }
- #endif // BENCHMARK_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement