{"by":"GeorgeHaldane","descendants":11,"id":43680477,"kids":[43693369,43691457,43686700,43683295,43683068,43683901,43684707],"score":68,"text":"Morning HN.<p>I often found myself wondering &quot;how much does this code segment take in terms of total runtime&quot; and it&#x27;s often quite annoying to figure out with optimizations enabled, especially when working on something new or testing someone else&#x27;s implementation without the proper tooling set up. Wanted to have a single include lib that would allow us to write something like:<p>```\nPROFILE(&quot;Loop 1&quot;)\nfor (...) &#x2F;&#x2F; some work\n```<p>and have the next expression automatically record time &amp; 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.<p>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&#x27;d need some kind of a recursive map of nodes with callsites as a keys.<p>Recently had a revelation that it is possible to use macro-generated thread_local&#x27;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 &amp; traversal logic reduced to simple checks and array lookups. Realized threading can be quite easily supported too in an almost lock-free fashion.<p>After a few days of effort ended up building what I believe is a very much usable single-header profiling lib. Couldn&#x27;t find anything quite like it, so I&#x27;d like to present it here and hear some opinions on the product:<p><a href=\"https:&#x2F;&#x2F;github.com&#x2F;DmitriBogdanov&#x2F;UTL&#x2F;blob&#x2F;master&#x2F;docs&#x2F;module_profiler.md\">https:&#x2F;&#x2F;github.com&#x2F;DmitriBogdanov&#x2F;UTL&#x2F;blob&#x2F;master&#x2F;docs&#x2F;modul...</a>","time":1744632963,"title":"Show HN: Single-Header Profiler for C++17","type":"story","url":"https://github.com/DmitriBogdanov/UTL/blob/master/docs/module_profiler.md"}