在Debian上进行GCC性能测试,可按以下步骤操作:
sudo apt update
sudo apt install gprof valgrind linux-tools-common linux-tools-generic # 安装gprof、valgrind、perf
test.c。gcc -pg -o test test.c # -pg选项生成性能分析数据
gcc -O2 -o test_optimized test.c # 使用-O2/-O3优化
./test # 生成gmon.out(gprof)或perf.data(perf)
gprof ./test gmon.out > analysis.txt # 查看函数调用耗时
sudo perf record -g ./test
sudo perf report # 查看CPU性能瓶颈
valgrind --tool=massif ./test # 分析内存使用
time命令对比不同优化级别的执行时间:time ./test
time ./test_optimized
sysbench、SuperPI)测试CPU/内存性能。说明:
gprof适合函数级性能分析,perf适合系统级(CPU/内存/磁盘)分析,Valgrind侧重内存问题检测。-O2、-O3)可显著影响性能,需结合测试结果调整。参考来源: