Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created October 1, 2015 22:32
Show Gist options
  • Save goldsborough/9f2f2f75fbae7a010e41 to your computer and use it in GitHub Desktop.
Save goldsborough/9f2f2f75fbae7a010e41 to your computer and use it in GitHub Desktop.
C++ benchmarking code
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