Mercurial > lbo > hg > ccplay
view lib/exec.cc @ 2:ed80bcd28852
cclib
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 06 Mar 2023 10:13:49 +0100 |
parents | ec6009463e1c |
children | a1ac40ee3a1e |
line wrap: on
line source
#include "exec.h" #include <chrono> #include <iostream> using namespace std; using namespace std::chrono; void timeit(function<void()> f, string name) { auto begin = high_resolution_clock::now(); f(); auto after = high_resolution_clock::now(); auto d = after - begin; decltype(d)::rep count = d.count(); decltype(d)::period p; cout << name << " :: " << static_cast<double>(p.num)/p.den * count << endl; } void benchmarkit(function<void()> f, string name, function<void()> setup) { int n = 1; const double min_duration = 0.1; while (true) { setup(); cout << "Running " << name << " for " << n << " iterations.\n"; auto begin = high_resolution_clock::now(); for (unsigned int i = 0; i < n; i += 1) f(); auto end = high_resolution_clock::now(); decltype(end-begin)::period p; auto seconds = (end-begin).count() * static_cast<double>(p.num)/p.den; if (seconds < min_duration) { n *= 2; } else { cout << n << " iterations took " << seconds << " seconds (" << seconds/n << " s/iter)\n"; break; } } }