[HN Gopher] Show HN: Single-Header Profiler for C++17
___________________________________________________________________
Show HN: Single-Header Profiler for C++17
Morning HN. I often found myself wondering "how much does this
code segment take in terms of total runtime" and it's often quite
annoying to figure out with optimizations enabled, especially when
working on something new or testing someone else's implementation
without the proper tooling set up. Wanted to have a single include
lib that would allow us to write something like: ``` PROFILE("Loop
1") for (...) // some work ``` and have the next expression
automatically record time & dump results to a table. Wrote a few
macros to do exactly that a few months back, but they were
primitive and basically unusable for recursive code. Tried to come
up with a more generic solution that would build a call graph for
nested profiler-macros, handle threads and etc. but doing so in a
naive way would be super slow since we'd need some kind of a
recursive map of nodes with callsites as a keys. Recently had a
revelation that it is possible to use macro-generated
thread_local's to associate callsites with integer IDs on the fly
and with some effort call graph can be neatly encoded in a few
contiguous arrays with all graph building & traversal logic reduced
to simple checks and array lookups. Realized threading can be quite
easily supported too in an almost lock-free fashion. After a few
days of effort ended up building what I believe is a very much
usable single-header profiling lib. Couldn't find anything quite
like it, so I'd like to present it here and hear some opinions on
the product:
https://github.com/DmitriBogdanov/UTL/blob/master/docs/modul...
Author : GeorgeHaldane
Score : 51 points
Date : 2025-04-14 12:16 UTC (10 hours ago)
(HTM) web link (github.com)
(TXT) w3m dump (github.com)
| a_e_k wrote:
| Also discussed on /r/cpp:
| https://www.reddit.com/r/cpp/comments/1jy6ver/utlprofiler_si...
| brosco wrote:
| This looks great! I've been needing something like this for a
| while, for a project which is quite compute-heavy and uses lots
| of threads and recursion. I've been using valgrind to profile
| small test examples, but that's definitely the nuclear option
| since it slows down the execution so much. I'm going to try this
| out right away.
| omoikane wrote:
| Do you also have some tools or scripts to help annotate code?
|
| One inconvenience with this library's approach is having to
| modify the code to add/remove instrumentation, compared to
| something like GNU gprof which has compiler support and doesn't
| require modifying the code.
| GeorgeHaldane wrote:
| I've though about this but had yet to come up with a simple
| approach, perhaps something like a python script hooked to GCC-
| XML can do the trick, will look into that in the future.
| Jeaye wrote:
| Great work! The colored, structured output is clean! Folks may
| also be interested in nanobench, which is also a single header
| C++ lib. It focuses on benchmarking blocks of code, though.
| bogwog wrote:
| How does the compare to Microprofile?
|
| https://github.com/jonasmr/microprofile
|
| Btw, I recently worked with a library that had their own profiler
| which generated a Chrome trace file, so you could load it up in
| the Chrome dev tools to explore the call graph and timings in a
| fancy UI.
|
| It seems like such a good idea and I wish more profiling
| frameworks tried to do that instead of building their own UI.
___________________________________________________________________
(page generated 2025-04-14 23:01 UTC)