23#ifndef VULPES_UTILS_PROFILER_HPP_
24#define VULPES_UTILS_PROFILER_HPP_
32#include <unordered_map>
36#include <nlohmann/json.hpp>
44#define PROFILE(func) \
46 Profiler::start(#func); \
48 Profiler::stop(#func); \
56#define PROFILE_INTERVAL(func) \
58 Profiler::startInterval(#func); \
60 Profiler::stopInterval(#func); \
84 static void start(
const char* label) {
86 start(std::string(label));
90 static void stop(
const char* label) {
92 stop(std::string(label));
116 static void start(
const std::string& label);
135 static void stop(
const std::string& label);
179 static size_t getDuration(
const std::string& label);
184 static void start(
const char*) {}
185 static void stop(
const char*) {}
188 static void start(
const std::string&) {}
189 static void stop(
const std::string&) {}
192 static void print() {}
193 static void clear() {}
194 static nlohmann::json
dumpJson() {
return {}; }
195 static size_t getDuration(
const std::string&) {
return -1; };
201 using time_point = std::chrono::high_resolution_clock::time_point;
205 static std::unordered_map<std::string, TimeCounter>
time_map;
207 static std::unordered_map<std::string, time_point>
start_times;
210 static std::unordered_map<std::string, std::vector<time_point>>
213 static std::unordered_map<std::string, std::vector<time_point>>
Collects high-resolution timing data for labeled code sections.
Definition profiler.hpp:73
static bool s_enabled
Definition profiler.hpp:199
std::pair< std::size_t, std::size_t > TimeCounter
Definition profiler.hpp:202
static void startInterval(const char *label)
startInterval(label) when recording is on.
Definition profiler.hpp:96
static size_t getDuration(const std::string &label)
Get the time elapsed for a certain label.
Definition profiler.cpp:187
static std::unordered_map< std::string, std::vector< time_point > > start_time_intervals
Interval start timestamps recorded per label via startInterval().
Definition profiler.hpp:48
static void stop(const char *label)
stop(label) when recording is on.
Definition profiler.hpp:90
static void clear()
Remove all recorded timing data.
Definition profiler.cpp:153
static std::unordered_map< std::string, time_point > start_times
Pending start time recorded per label via start(), consumed by stop().
Definition profiler.hpp:39
static void stopInterval(const char *label)
stopInterval(label) when recording is on.
Definition profiler.hpp:102
static nlohmann::json dumpJson()
Export collected profile data as a JSON object including interval timestamps.
Definition profiler.cpp:159
static std::unordered_map< std::string, TimeCounter > time_map
Definition profiler.hpp:37
static std::unordered_map< std::string, std::vector< time_point > > stop_time_intervals
Interval end timestamps recorded per label via stopInterval().
Definition profiler.hpp:44
static void print()
Print accumulated timings for all labels in milliseconds.
Definition profiler.cpp:91
static void setEnabled(bool enabled)
Turn recording on or off (on by default), outside timed regions: a stop() without its start() records...
Definition profiler.hpp:80
static void start(const char *label)
start(label) when recording is on.
Definition profiler.hpp:84
std::chrono::high_resolution_clock::time_point time_point
Definition profiler.hpp:201