跳转到内容

性能分析入门

性能优化应从测量开始:先用性能分析工具定位瓶颈(CPU、GPU、内存或 IO),再有针对性地优化。本章介绍帧时间预算、常用分析工具和简单的插桩计时方法。

// 示例:简单的作用域计时器
struct ScopedTimer {
const char* name;
std::chrono::high_resolution_clock::time_point start =
std::chrono::high_resolution_clock::now();
~ScopedTimer() {
auto us = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - start).count();
printf("[%s] %lld us\n", name, (long long)us);
}
};
#define PROFILE_SCOPE(n) ScopedTimer _timer{n}