通过使用gprof可以测试程序库的性能。
在编译代码时加入 -pg选项即可。如下所示
g++ main.cpp -L. -lCompute -g -pg -o infosomain
之后运行程序 ./infosomain 之后生成 gmon.out文件。
对于gmon.out文件需要通过gropf命令打开。
gprof infosomain gmon.out -b >a.txt (将输出的信息重定向到文件中)
查看
查看程序运行信息的命令是gprof,它以gmon.out文件作为输入,也就是将gmon.out文件翻译成可读的形式展现给用户。其命令格式如下:
gprof [可执行文件] [gmon.out文件] [其它参数]
方括号中的内容可以省略。如果省略了“可执行文件”,gprof会在当前目录下搜索a.out文件作为可执行文件,而如果省略了gmon.out文件,gprof也会在当前目录下寻找gmon.out。其它参数可以控制gprof输出内容的格式等信息。最常用的参数如下:
l-b 不再输出统计图表中每个字段的详细描述。
l-p 只输出函数的调用图(Call graph的那部分信息)。
l-q 只输出函数的时间消耗列表。
l-e Name 不再输出函数Name 及其子函数的调用图(除非它们有未被限制的其它父函数)。可以给定多个 -e 标志。一个 -e 标志只能指定一个函数。
l-E Name 不再输出函数Name 及其子函数的调用图,此标志类似于 -e 标志,但它在总时间和百分比时间的计算中排除了由函数Name 及其子函数所用的时间。
l-f Name 输出函数Name 及其子函数的调用图。可以指定多个 -f 标志。一个 -f 标志只能指定一个函数。
l-F Name 输出函数Name 及其子函数的调用图,它类似于 -f 标志,但它在总时间和百分比时间计算中仅使用所打印的例程的时间。可以指定多个 -F 标志。一个 -F 标志只能指定一个函数。-F 标志覆盖 -E 标志。
l-z 显示使用次数为零的例程(按照调用计数和累积时间计算)。
gprof产生的信息
% the percentage of the total running time of the
time program used by this function.
函数使用时间占所有时间的百分比。
cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.
函数和上列函数累计执行的时间。
self the number of seconds accounted for by this
seconds function alone. This is the major sort for this
listing.
函数本身所执行的时间。
calls the number of times this function was invoked, if
this function is profiled, else blank.
函数被调用的次数
self the average number of milliseconds spent in this
ms/call function per call, if this function is profiled,
else blank.
每一次调用花费在函数的时间microseconds。
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
每一次调用,花费在函数及其衍生函数的平均时间microseconds。
name the name of the function. This is the minor sort
for this listing. The index shows the location of
the function in the gprof listing. If the index is
in parenthesis it shows where it would appear in
the gprof listing if it were to be printed.
函数名
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 1001 0.00 0.00 std::vector
0.00 0.00 0.00 1000 0.00 0.00 std::vector
0.00 0.00 0.00 14 0.00 0.00 void std::_Destroy_aux
0.00 0.00 0.00 14 0.00 0.00 void std::_Destroy
0.00 0.00 0.00 13 0.00 0.00 void std::_Destroy_aux
0.00 0.00 0.00 13 0.00 0.00 void std::_Destroy
0.00 0.00 0.00 12 0.00 0.00 __gnu_cxx::new_allocator
0.00 0.00 0.00 11 0.00 0.00 __gnu_cxx::new_allocator
0.00 0.00 0.00 2 0.00 0.00 __gnu_cxx::new_allocator
0.00 0.00 0.00 2 0.00 0.00 __gnu_cxx::new_allocator
0.00 0.00 0.00 2 0.00 0.00 __gnu_cxx::new_allocator
0.00 0.00 0.00 2 0.00 0.00 __gnu_cxx::new_allocator
0.00 0.00 0.00 2 0.00 0.00 std::allocator
0.00 0.00 0.00 2 0.00 0.00 std::allocator
0.00 0.00 0.00 2 0.00 0.00 std::allocator
0.00 0.00 0.00 2 0.00 0.00 std::allocator
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::_Vector_base
0.00 0.00 0.00 2 0.00 0.00 std::vector
0.00 0.00 0.00 2 0.00 0.00 std::vector
0.00 0.00 0.00 2 0.00 0.00 void std::_Destroy
0.00 0.00 0.00 2 0.00 0.00 void std::_Destroy
0.00 0.00 0.00 1 0.00 0.00 _GLOBAL__sub_I_main
0.00 0.00 0.00 1 0.00 0.00 __static_initialization_and_destruction_0(int, int)
0.00 0.00 0.00 1 0.00 0.00 Calculate::Calculate()
0.00 0.00 0.00 1 0.00 0.00 Calculate::~Calculate()
0.00 0.00 0.00 1 0.00 0.00 std::vector
0.00 0.00 0.00 1 0.00 0.00 std::vector
Call graph
granularity: each sample hit covers 2 byte(s) no time propagated
index % time self children called name
0.00 0.00 1001/1001 main [6]
[8] 0.0 0.00 0.00 1001 std::vector
-----------------------------------------------
0.00 0.00 1000/1000 main [6]
[9] 0.0 0.00 0.00 1000 std::vector
-----------------------------------------------
0.00 0.00 14/14 void std::_Destroy
[10] 0.0 0.00 0.00 14 void std::_Destroy_aux
-----------------------------------------------
0.00 0.00 14/14 void std::_Destroy
[11] 0.0 0.00 0.00 14 void std::_Destroy
0.00 0.00 14/14 void std::_Destroy_aux
-----------------------------------------------
0.00 0.00 13/13 void std::_Destroy
[12] 0.0 0.00 0.00 13 void std::_Destroy_aux
-----------------------------------------------
0.00 0.00 13/13 void std::_Destroy
[13] 0.0 0.00 0.00 13 void std::_Destroy
0.00 0.00 13/13 void std::_Destroy_aux
-----------------------------------------------
0.00 0.00 12/12 std::_Vector_base
[14] 0.0 0.00 0.00 12 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 11/11 std::_Vector_base
[15] 0.0 0.00 0.00 11 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::allocator
[16] 0.0 0.00 0.00 2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::allocator
[17] 0.0 0.00 0.00 2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::allocator
[18] 0.0 0.00 0.00 2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::allocator
[19] 0.0 0.00 0.00 2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[20] 0.0 0.00 0.00 2 std::allocator
0.00 0.00 2/2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[21] 0.0 0.00 0.00 2 std::allocator
0.00 0.00 2/2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[22] 0.0 0.00 0.00 2 std::allocator
0.00 0.00 2/2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[23] 0.0 0.00 0.00 2 std::allocator
0.00 0.00 2/2 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[24] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[25] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[26] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 12/12 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::vector
[27] 0.0 0.00 0.00 2 std::_Vector_base
-----------------------------------------------
0.00 0.00 2/2 std::vector
[28] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
0.00 0.00 2/2 std::vector
[29] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::_Vector_base
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[30] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[31] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::allocator
-----------------------------------------------
0.00 0.00 2/2 std::_Vector_base
[32] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 11/11 __gnu_cxx::new_allocator
-----------------------------------------------
0.00 0.00 2/2 std::vector
[33] 0.0 0.00 0.00 2 std::_Vector_base
-----------------------------------------------
0.00 0.00 2/2 std::vector
[34] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
0.00 0.00 2/2 std::vector
[35] 0.0 0.00 0.00 2 std::_Vector_base
0.00 0.00 2/2 std::_Vector_base
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
0.00 0.00 1/2 main [6]
0.00 0.00 1/2 Calculate::Calculate() [42]
[36] 0.0 0.00 0.00 2 std::vector
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
0.00 0.00 1/2 main [6]
0.00 0.00 1/2 Calculate::~Calculate() [43]
[37] 0.0 0.00 0.00 2 std::vector
0.00 0.00 2/2 std::_Vector_base
0.00 0.00 2/2 void std::_Destroy
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
0.00 0.00 2/2 std::vector
[38] 0.0 0.00 0.00 2 void std::_Destroy
0.00 0.00 14/14 void std::_Destroy
-----------------------------------------------
0.00 0.00 2/2 std::vector
[39] 0.0 0.00 0.00 2 void std::_Destroy
0.00 0.00 13/13 void std::_Destroy
-----------------------------------------------
0.00 0.00 1/1 __libc_csu_init [51]
[40] 0.0 0.00 0.00 1 _GLOBAL__sub_I_main [40]
0.00 0.00 1/1 __static_initialization_and_destruction_0(int, int) [41]
-----------------------------------------------
0.00 0.00 1/1 _GLOBAL__sub_I_main [40]
[41] 0.0 0.00 0.00 1 __static_initialization_and_destruction_0(int, int) [41]
-----------------------------------------------
0.00 0.00 1/1 main [6]
[42] 0.0 0.00 0.00 1 Calculate::Calculate() [42]
0.00 0.00 1/1 std::vector
0.00 0.00 1/2 std::vector
-----------------------------------------------
0.00 0.00 1/1 main [6]
[43] 0.0 0.00 0.00 1 Calculate::~Calculate() [43]
0.00 0.00 1/1 std::vector
0.00 0.00 1/2 std::vector
-----------------------------------------------
0.00 0.00 1/1 Calculate::Calculate() [42]
[44] 0.0 0.00 0.00 1 std::vector
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
0.00 0.00 1/1 Calculate::~Calculate() [43]
[45] 0.0 0.00 0.00 1 std::vector
0.00 0.00 2/2 std::_Vector_base
0.00 0.00 2/2 void std::_Destroy
0.00 0.00 2/2 std::_Vector_base
-----------------------------------------------
Index by function name
[40] _GLOBAL__sub_I_main (main.cpp) [22] std::allocator
[41] __static_initialization_and_destruction_0(int, int) (main.cpp) [23] std::allocator
[42] Calculate::Calculate() [10] void std::_Destroy_aux
[43] Calculate::~Calculate() [12] void std::_Destroy_aux
[14] __gnu_cxx::new_allocator
[16] __gnu_cxx::new_allocator
[17] __gnu_cxx::new_allocator
[15] __gnu_cxx::new_allocator
[18] __gnu_cxx::new_allocator
[19] __gnu_cxx::new_allocator
[8] std::vector
[20] std::allocator
[21] std::allocator