searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

GCC中-pg选项的作用及引发的性能问题

2023-06-26 09:07:34
63
0

通过man gcc查询到选项含义:

-p  Generate extra code to write profile information suitable for the analysis program prof.  You must use this option when compiling the source files you want data about, and you must also use it when linking.

 

-pg  Generate extra code to write profile information suitable for the analysis program gprof.  You must use this option when compiling the source files you want data about, and you must also use it when linking.

简单说,-p选项在编译和链接二进制文件时附加多余信息,并直接插入二进制文件中(比如函数调用前和调用后,额外调用编译器内置的其它跟踪函数),生成的是通用格式文件。

-pg选项区别是生成的是通过gprof分析的文件gmon.out。gcc通过在每个函数调用前和调用结束增加mcount函数,mcount函数用于统计函数运行时延。那么必然会导致性能下降。

举个项目中使用贪心算法在使用-pg选项编译和链接后,并使用stl库中的内存管理导致运行超过20s的例子分析:

  1. 通过运行贪心算法程序,在运行该程序的目录下生成了out文件
  2. 通过命令:gprof “二进制执行文件”out > res.txt
  3. 检查txt文件中的内容,发现无耗时很长的函数。
  4. 通过命令: perf record “二进制执行文件”和perf report -i perf.data发现存在在res.txt中不存在的函数mcount,并且占用比例奇高。
  5. 当我们不使用stl来管理内存时,性能也好很多。其中也可以看到很多stl以及相关的数据结构拷贝构造,析构函数的调用。耗时从20s+下降到6秒左右。
  6. 当我们采用Release编译,没有相关的debug选项,开启-O2优化后,目录下没有生成gmon.out文件,通过perf工具可以看到无新加的mcount,并且整体耗时继续下降到不足100ms,其中还包括比较耗时的日志打印。
0条评论
0 / 1000
d****n
2文章数
0粉丝数
d****n
2 文章 | 0 粉丝
d****n
2文章数
0粉丝数
d****n
2 文章 | 0 粉丝
原创

GCC中-pg选项的作用及引发的性能问题

2023-06-26 09:07:34
63
0

通过man gcc查询到选项含义:

-p  Generate extra code to write profile information suitable for the analysis program prof.  You must use this option when compiling the source files you want data about, and you must also use it when linking.

 

-pg  Generate extra code to write profile information suitable for the analysis program gprof.  You must use this option when compiling the source files you want data about, and you must also use it when linking.

简单说,-p选项在编译和链接二进制文件时附加多余信息,并直接插入二进制文件中(比如函数调用前和调用后,额外调用编译器内置的其它跟踪函数),生成的是通用格式文件。

-pg选项区别是生成的是通过gprof分析的文件gmon.out。gcc通过在每个函数调用前和调用结束增加mcount函数,mcount函数用于统计函数运行时延。那么必然会导致性能下降。

举个项目中使用贪心算法在使用-pg选项编译和链接后,并使用stl库中的内存管理导致运行超过20s的例子分析:

  1. 通过运行贪心算法程序,在运行该程序的目录下生成了out文件
  2. 通过命令:gprof “二进制执行文件”out > res.txt
  3. 检查txt文件中的内容,发现无耗时很长的函数。
  4. 通过命令: perf record “二进制执行文件”和perf report -i perf.data发现存在在res.txt中不存在的函数mcount,并且占用比例奇高。
  5. 当我们不使用stl来管理内存时,性能也好很多。其中也可以看到很多stl以及相关的数据结构拷贝构造,析构函数的调用。耗时从20s+下降到6秒左右。
  6. 当我们采用Release编译,没有相关的debug选项,开启-O2优化后,目录下没有生成gmon.out文件,通过perf工具可以看到无新加的mcount,并且整体耗时继续下降到不足100ms,其中还包括比较耗时的日志打印。
文章来自个人专栏
文章 | 订阅
0条评论
0 / 1000
请输入你的评论
0
0