Created
October 1, 2015 22:32
-
-
Save goldsborough/9f2f2f75fbae7a010e41 to your computer and use it in GitHub Desktop.
C++ benchmarking code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Function, typename... Args> | |
auto benchmark(const std::size_t runs, Function function, Args&&... args) | |
{ | |
using duration_t = std::chrono::duration<double, std::milli>; | |
using clock = std::chrono::high_resolution_clock; | |
duration_t duration(0); | |
for (std::size_t i = 0; i < runs; ++i) | |
{ | |
auto start = clock::now(); | |
function(std::forward<Args>(args)...); | |
duration += (clock::now() - start); | |
} | |
return (duration/runs).count(); | |
} | |
template<typename Return, typename... All, typename... Args> | |
auto benchmark(Return (&function) (All...), Args&&... args) | |
{ | |
return benchmark(1e6, function, std::forward<Args>(args)...); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment