Linux下程序的Profile工具
}
命令行里面输入下面命令,没加-c选项,gcc 会默认进行编译并链接生成a.out:
引文:
[linux /home/test]$gcc -pg test.c
如果没有编译错误,gcc会在当前目录下生成一个a.out文件,当然你也可以使用 –o 选项给生成的文件起一个别的名字,像 gcc –pg test.c –o test , 则gcc会生成一个名为test的可执行文件,在命令行下输入[linux /home/test]$./test ,就可以执行该程序了,记住一定要加上 ./ 否则程序看上去可能是执行,可是什么输出都没有。
2.执行你的应用程序使之生成供gprof 分析的数据。 命令行里面输入:
引文:
[linux /home/test]$a.out
main() function()
+--- call b() function
+---call a() function
+---call c() function
[linux /home/test]$
你会在当前目录下看到一个gmon.out 文件, 这个文件就是供gprof 分析使用的。
3.使用gprof 程序分析你的应用程序生成的数据。
命令行里面输入:
引文:
[linux /home/test]$ gprof -b a.out gmon.out | less
由于gprof输出的信息比较多,这里使用了 less 命令,该命令可以让我们通过上下方向键查看gprof产生的输出,|表示gprof -b a.out gmon.out 的输出作为 less的输入。下面是我从gprof输出中摘抄出的与我们有关的一些详细信息。
引文:
Flat profile:
Each sample counts as 0.01 seconds.
no time accumulated
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1 0.00 0.00 a
0.00 0.00 0.00 1 0.00 0.00 b
0.00 0.00 0.00 1 0.00 0.00 c
Call graph
granularity: each sample hit covers 4 byte(s) no time propagated
index % time self children called name
0.00 0.00 1/1 b [2]
[1] 0.0 0.00 0.00 1 a [1]
-----------------------------------------------
0.00 0.00 1/1 main [10]
[2] 0.0 0.00 0.00 1 b [2]
0.00 0.00 1/1 c [3]
0.00 0.00 1/1 a [1]
-----------------------------------------------
0.00 0.00 1/1 b [2]
[3] 0.0 0.00 0.00 1 c [3]
-----------------------------------------------
Index by function name
[1] a [2] b [3] c
从上面的输出我们能明显的看出来,main 调用了 b 函数, 而b 函数分别调用了a 和 c 函数。由于我们的函数只是简单的输出了一个字串,故每个函数的消耗时间都是0 秒。
【Linux下程序的Profile工具】相关文章: