Show HN: Single-Header Profiler for C++17

https://news.ycombinator.com/rss Hits: 6
Summary

<- to README.md <- to implementation.hpp utl::profiler is single-include solution for localized profiling, it features simple macros to measure how much time is taken by a certain scope / expression / code segment. Profiler automatically builds a call graph for all profiled functions and prints a nicely formatted table for every thread. See examples. Key features: Below is an output example from profiling a JSON parser: // Profiling macros UTL_PROFILER_SCOPE(label); UTL_PROFILER(label); UTL_PROFILER_BEGIN(segment, label); UTL_PROFILER_END(segment); // Style options struct Style { std::size_t indent = 2; bool color = true; double cutoff_red = 0.40; // > 40% of total runtime double cutoff_yellow = 0.20; // > 20% of total runtime double cutoff_gray = 0.01; // < 1% of total runtime }; // Global profiler object struct Profiler { void print_at_exit(bool value) noexcept; void upload_this_thread(); std::string format_results(const Style& style = Style{}); }; inline Profiler profiler; UTL_PROFILER_SCOPE(label); Attaches profiler to the current scope. If profiled scope was entered at any point of the program, upon exiting main() a per-thread call graph will be built for all profiled segments. Note 1: label is a string literal name that will be shown in the results table. Note 2: Automatic printing on exit can be disabled. Attaches profiler to the scope of the following expression. Convenient to profile individual loops / function calls / ifs and etc. UTL_PROFILER_BEGIN(segment, label); UTL_PROFILER_END(segment); Attaches profiler to the code section between two BEGIN/END macros with the same segment label. struct Style { std::size_t indent = 2; bool color = true; double cutoff_red = 0.40; // > 40% of total runtime double cutoff_yellow = 0.20; // > 20% of total runtime double cutoff_gray = 0.01; // < 1% of total runtime }; A struct with formatting settings for Profiler::format_results(). void Profiler::print_at_exit(bool value) noexcept; Sets whether profiling results should b...

First seen: 2025-04-14 17:05

Last seen: 2025-04-14 22:06