(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43680477

DmitriBogdanov 提供了一个单头文件的 C++17 性能分析库,用于快速测量代码段的运行时间。该库解决了普遍存在的需要理解特定代码段性能影响的需求,尤其是在启用了优化且缺少合适的工具的情况下。 该库使用类似 `PROFILE("Loop 1")` 的宏来自动记录后续表达式的运行时间,并将结果输出到表格中。与更简单的基于宏的解决方案不同,该库支持递归代码并处理线程。它利用宏生成的线程局部变量来有效地将调用站点与整数 ID 关联,从而能够在连续数组中构建调用图。这种方法通过依赖数组查找和无锁线程支持来最大限度地减少开销。 创建者认为生成的库是独一无二且高度易用的。他寻求 Hacker News 社区的反馈,以了解该项目的设想和功能。


原文
Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Single-Header Profiler for C++17 (github.com/dmitribogdanov)
6 points by GeorgeHaldane 1 hour ago | hide | past | favorite | 1 comment
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...

















Join us for AI Startup School this June 16-17 in San Francisco!


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



Search:
联系我们 contact @ memedata.com