Mercurial > lbo > hg > ccplay
view lib/exec.cc @ 24:b89680a8af68 default tip
graph: detect negative cycles
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 09 Apr 2023 17:27:20 +0200 |
parents | 90bf264f80a5 |
children |
line wrap: on
line source
#include "exec.h" 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; fmt::print("{} :: {} s\n", name, static_cast<double>(p.num)/p.den * count); } void benchmarkit(function<void()> f, string name, function<void()> setup) { int n = 1; const double min_duration = 0.1; while (true) { setup(); fmt::print("Running {} for {} iterations\n", name, 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 { fmt::print("{} iterations took {} seconds ({} s/iter)\n", n, seconds, seconds/n); break; } } }