view lib/exec.cc @ 23:c3fb5f666e2d

graphs: Two graph algorithms
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 09 Apr 2023 17:17:04 +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;
        }
    }
}