Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function timing #47

Open
PhilipDeegan opened this issue Apr 11, 2024 · 0 comments
Open

function timing #47

PhilipDeegan opened this issue Apr 11, 2024 · 0 comments

Comments

@PhilipDeegan
Copy link
Member

essentially

#include <string_view>
#include <cstdint>
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>

#define PRINT(...) std::cout << __FILE__ << " " << __LINE__ << " " << __VA_ARGS__ << std::endl;

std::uint64_t now() {
    return std::chrono::duration_cast<std::chrono::milliseconds>(
               std::chrono::system_clock::now().time_since_epoch())
        .count();
}

struct Report;

static std::vector<Report*> reports;

struct Report {
    std::string_view s;
    std::uint32_t l = 0;

    Report(std::string_view const& sv, std::uint32_t const& _l) : s{sv}, l{_l} {
        reports.emplace_back(this);
    }

    std::vector<std::uint32_t> times;
};

struct scope_timer {
    Report& r;
    std::uint64_t const start = now();
    ~scope_timer() noexcept { r.times.emplace_back(now() - start); }
};

#define SCOPE_TIMER                                    \
    static Report ridx_##__LINE__{__FILE__, __LINE__}; \
    scope_timer _scope_timer_##__LINE__{ridx_##__LINE__};

void fn0() {
    SCOPE_TIMER;
    using namespace std::chrono_literals;
    std::this_thread::sleep_for(100ms);
}

void fn1() {
    SCOPE_TIMER;
    using namespace std::chrono_literals;
    std::this_thread::sleep_for(100ms);
    fn0();
}

int main() {
    for (std::size_t i = 0; i < 5; ++i) fn1();

    for (auto const& rp : reports) {
        auto const& r = *rp;
        PRINT(r.s << " " << r.l);
        for (auto const& t : r.times) PRINT(t);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant