性能分析工具

一月 27, 2026 #perf

性能分析工具

gprof

##编译代码
g++ -pg -o myprogram hello.cpp
## 执行代码,生成调用文件
./myprogram
## 分析日志
gprof myprogram gmon.out > analysis.txt

perf

## 编译代码
g++ -O2 -o myprogram  hello.cpp
## 执行程序
perf record ./myprogram
## 分析结果
perf report

valgrind

## 直接执行程序 进行检查(就是执行有点慢)
valgrind --tool=memcheck ./myprogram
## 检测内存泄漏
valgrind --leak-check=full ./hello
## callgrind 工具可以用来收集程序运行时的函数调用信息,这对于性能分析非常有用。使用 callgrind 运行程序, 生成callgrind.out.<pid>文件
valgrind --tool=callgrind ./myprogram
# 使用 kcachegrind 工具来查看和分析这些数据:
kcachegrind callgrind.out.<pid>

内存泄漏检测

valgrind --leak-check=full ./your_program

堆栈跟踪

valgrind --tool=memcheck --trace-children=yes ./your_program

缓存模拟

valgrind --tool=cachegrind --I1=32k,2,8 --D1=32k,2,8 ./your_program

调用图分析

valgrind --tool=callgrind ./your_program
kcachegrind callgrind.out.<pid>

数据竞争检测

valgrind --tool=drd ./your_program