linux 程序性能测试

通过使用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<double, std::allocator<double> >::size() const
 0.00      0.00     0.00     1000     0.00     0.00  std::vector<double, std::allocator<double> >::operator[](unsigned long)
 0.00      0.00     0.00       14     0.00     0.00  void std::_Destroy_aux<true>::__destroy<double*>(double*, double*)
 0.00      0.00     0.00       14     0.00     0.00  void std::_Destroy<double*>(double*, double*)
 0.00      0.00     0.00       13     0.00     0.00  void std::_Destroy_aux<true>::__destroy<int*>(int*, int*)
 0.00      0.00     0.00       13     0.00     0.00  void std::_Destroy<int*>(int*, int*)
 0.00      0.00     0.00       12     0.00     0.00  __gnu_cxx::new_allocator<double>::deallocate(double*, unsigned long)
 0.00      0.00     0.00       11     0.00     0.00  __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)
 0.00      0.00     0.00        2     0.00     0.00  __gnu_cxx::new_allocator<double>::new_allocator()
 0.00      0.00     0.00        2     0.00     0.00  __gnu_cxx::new_allocator<double>::~new_allocator()
 0.00      0.00     0.00        2     0.00     0.00  __gnu_cxx::new_allocator<int>::new_allocator()
 0.00      0.00     0.00        2     0.00     0.00  __gnu_cxx::new_allocator<int>::~new_allocator()
 0.00      0.00     0.00        2     0.00     0.00  std::allocator<double>::allocator()
 0.00      0.00     0.00        2     0.00     0.00  std::allocator<double>::~allocator()
 0.00      0.00     0.00        2     0.00     0.00  std::allocator<int>::allocator()
 0.00      0.00     0.00        2     0.00     0.00  std::allocator<int>::~allocator()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<double, std::allocator<double> >::_Vector_impl::_Vector_impl()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<double, std::allocator<double> >::_Vector_impl::~_Vector_impl()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<double, std::allocator<double> >::_M_deallocate(double*, unsigned long)
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<double, std::allocator<double> >::_M_get_Tp_allocator()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<double, std::allocator<double> >::_Vector_base()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<double, std::allocator<double> >::~_Vector_base()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long)
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_M_get_Tp_allocator()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::_Vector_base()
 0.00      0.00     0.00        2     0.00     0.00  std::_Vector_base<int, std::allocator<int> >::~_Vector_base()
 0.00      0.00     0.00        2     0.00     0.00  std::vector<double, std::allocator<double> >::vector()
 0.00      0.00     0.00        2     0.00     0.00  std::vector<double, std::allocator<double> >::~vector()
 0.00      0.00     0.00        2     0.00     0.00  void std::_Destroy<double*, double>(double*, double*, std::allocator<double>&)
 0.00      0.00     0.00        2     0.00     0.00  void std::_Destroy<int*, int>(int*, int*, std::allocator<int>&)
 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<int, std::allocator<int> >::vector()
 0.00      0.00     0.00        1     0.00     0.00  std::vector<int, std::allocator<int> >::~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<double, std::allocator<double> >::size() const [8]
-----------------------------------------------
               0.00    0.00    1000/1000        main [6]
[9]      0.0    0.00    0.00    1000         std::vector<double, std::allocator<double> >::operator[](unsigned long) [9]
-----------------------------------------------
               0.00    0.00      14/14          void std::_Destroy<double*>(double*, double*) [11]
[10]     0.0    0.00    0.00      14         void std::_Destroy_aux<true>::__destroy<double*>(double*, double*) [10]
-----------------------------------------------
               0.00    0.00      14/14          void std::_Destroy<double*, double>(double*, double*, std::allocator<double>&) [38]
[11]     0.0    0.00    0.00      14         void std::_Destroy<double*>(double*, double*) [11]
               0.00    0.00      14/14          void std::_Destroy_aux<true>::__destroy<double*>(double*, double*) [10]
-----------------------------------------------
               0.00    0.00      13/13          void std::_Destroy<int*>(int*, int*) [13]
[12]     0.0    0.00    0.00      13         void std::_Destroy_aux<true>::__destroy<int*>(int*, int*) [12]
-----------------------------------------------
               0.00    0.00      13/13          void std::_Destroy<int*, int>(int*, int*, std::allocator<int>&) [39]
[13]     0.0    0.00    0.00      13         void std::_Destroy<int*>(int*, int*) [13]
               0.00    0.00      13/13          void std::_Destroy_aux<true>::__destroy<int*>(int*, int*) [12]
-----------------------------------------------
               0.00    0.00      12/12          std::_Vector_base<double, std::allocator<double> >::_M_deallocate(double*, unsigned long) [26]
[14]     0.0    0.00    0.00      12         __gnu_cxx::new_allocator<double>::deallocate(double*, unsigned long) [14]
-----------------------------------------------
               0.00    0.00      11/11          std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) [32]
[15]     0.0    0.00    0.00      11         __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) [15]
-----------------------------------------------
               0.00    0.00       2/2           std::allocator<double>::allocator() [20]
[16]     0.0    0.00    0.00       2         __gnu_cxx::new_allocator<double>::new_allocator() [16]
-----------------------------------------------
               0.00    0.00       2/2           std::allocator<double>::~allocator() [21]
[17]     0.0    0.00    0.00       2         __gnu_cxx::new_allocator<double>::~new_allocator() [17]
-----------------------------------------------
               0.00    0.00       2/2           std::allocator<int>::allocator() [22]
[18]     0.0    0.00    0.00       2         __gnu_cxx::new_allocator<int>::new_allocator() [18]
-----------------------------------------------
               0.00    0.00       2/2           std::allocator<int>::~allocator() [23]
[19]     0.0    0.00    0.00       2         __gnu_cxx::new_allocator<int>::~new_allocator() [19]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_Vector_impl::_Vector_impl() [24]
[20]     0.0    0.00    0.00       2         std::allocator<double>::allocator() [20]
               0.00    0.00       2/2           __gnu_cxx::new_allocator<double>::new_allocator() [16]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_Vector_impl::~_Vector_impl() [25]
[21]     0.0    0.00    0.00       2         std::allocator<double>::~allocator() [21]
               0.00    0.00       2/2           __gnu_cxx::new_allocator<double>::~new_allocator() [17]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl() [30]
[22]     0.0    0.00    0.00       2         std::allocator<int>::allocator() [22]
               0.00    0.00       2/2           __gnu_cxx::new_allocator<int>::new_allocator() [18]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl() [31]
[23]     0.0    0.00    0.00       2         std::allocator<int>::~allocator() [23]
               0.00    0.00       2/2           __gnu_cxx::new_allocator<int>::~new_allocator() [19]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_Vector_base() [28]
[24]     0.0    0.00    0.00       2         std::_Vector_base<double, std::allocator<double> >::_Vector_impl::_Vector_impl() [24]
               0.00    0.00       2/2           std::allocator<double>::allocator() [20]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::~_Vector_base() [29]
[25]     0.0    0.00    0.00       2         std::_Vector_base<double, std::allocator<double> >::_Vector_impl::~_Vector_impl() [25]
               0.00    0.00       2/2           std::allocator<double>::~allocator() [21]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::~_Vector_base() [29]
[26]     0.0    0.00    0.00       2         std::_Vector_base<double, std::allocator<double> >::_M_deallocate(double*, unsigned long) [26]
               0.00    0.00      12/12          __gnu_cxx::new_allocator<double>::deallocate(double*, unsigned long) [14]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<double, std::allocator<double> >::~vector() [37]
[27]     0.0    0.00    0.00       2         std::_Vector_base<double, std::allocator<double> >::_M_get_Tp_allocator() [27]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<double, std::allocator<double> >::vector() [36]
[28]     0.0    0.00    0.00       2         std::_Vector_base<double, std::allocator<double> >::_Vector_base() [28]
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_Vector_impl::_Vector_impl() [24]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<double, std::allocator<double> >::~vector() [37]
[29]     0.0    0.00    0.00       2         std::_Vector_base<double, std::allocator<double> >::~_Vector_base() [29]
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_M_deallocate(double*, unsigned long) [26]
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_Vector_impl::~_Vector_impl() [25]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_Vector_base() [34]
[30]     0.0    0.00    0.00       2         std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl() [30]
               0.00    0.00       2/2           std::allocator<int>::allocator() [22]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::~_Vector_base() [35]
[31]     0.0    0.00    0.00       2         std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl() [31]
               0.00    0.00       2/2           std::allocator<int>::~allocator() [23]
-----------------------------------------------
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::~_Vector_base() [35]
[32]     0.0    0.00    0.00       2         std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) [32]
               0.00    0.00      11/11          __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) [15]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<int, std::allocator<int> >::~vector() [45]
[33]     0.0    0.00    0.00       2         std::_Vector_base<int, std::allocator<int> >::_M_get_Tp_allocator() [33]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<int, std::allocator<int> >::vector() [44]
[34]     0.0    0.00    0.00       2         std::_Vector_base<int, std::allocator<int> >::_Vector_base() [34]
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl() [30]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<int, std::allocator<int> >::~vector() [45]
[35]     0.0    0.00    0.00       2         std::_Vector_base<int, std::allocator<int> >::~_Vector_base() [35]
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) [32]
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl() [31]
-----------------------------------------------
               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<double, std::allocator<double> >::vector() [36]
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_Vector_base() [28]
-----------------------------------------------
               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<double, std::allocator<double> >::~vector() [37]
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::_M_get_Tp_allocator() [27]
               0.00    0.00       2/2           void std::_Destroy<double*, double>(double*, double*, std::allocator<double>&) [38]
               0.00    0.00       2/2           std::_Vector_base<double, std::allocator<double> >::~_Vector_base() [29]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<double, std::allocator<double> >::~vector() [37]
[38]     0.0    0.00    0.00       2         void std::_Destroy<double*, double>(double*, double*, std::allocator<double>&) [38]
               0.00    0.00      14/14          void std::_Destroy<double*>(double*, double*) [11]
-----------------------------------------------
               0.00    0.00       2/2           std::vector<int, std::allocator<int> >::~vector() [45]
[39]     0.0    0.00    0.00       2         void std::_Destroy<int*, int>(int*, int*, std::allocator<int>&) [39]
               0.00    0.00      13/13          void std::_Destroy<int*>(int*, int*) [13]
-----------------------------------------------
               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<int, std::allocator<int> >::vector() [44]
               0.00    0.00       1/2           std::vector<double, std::allocator<double> >::vector() [36]
-----------------------------------------------
               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<int, std::allocator<int> >::~vector() [45]
               0.00    0.00       1/2           std::vector<double, std::allocator<double> >::~vector() [37]
-----------------------------------------------
               0.00    0.00       1/1           Calculate::Calculate() [42]
[44]     0.0    0.00    0.00       1         std::vector<int, std::allocator<int> >::vector() [44]
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_Vector_base() [34]
-----------------------------------------------
               0.00    0.00       1/1           Calculate::~Calculate() [43]
[45]     0.0    0.00    0.00       1         std::vector<int, std::allocator<int> >::~vector() [45]
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::_M_get_Tp_allocator() [33]
               0.00    0.00       2/2           void std::_Destroy<int*, int>(int*, int*, std::allocator<int>&) [39]
               0.00    0.00       2/2           std::_Vector_base<int, std::allocator<int> >::~_Vector_base() [35]
-----------------------------------------------


Index by function name

 [40] _GLOBAL__sub_I_main (main.cpp) [22] std::allocator<int>::allocator() [33] std::_Vector_base<int, std::allocator<int> >::_M_get_Tp_allocator()
 [41] __static_initialization_and_destruction_0(int, int) (main.cpp) [23] std::allocator<int>::~allocator() [34] std::_Vector_base<int, std::allocator<int> >::_Vector_base()
 [42] Calculate::Calculate() [10] void std::_Destroy_aux<true>::__destroy<double*>(double*, double*) [35] std::_Vector_base<int, std::allocator<int> >::~_Vector_base()
 [43] Calculate::~Calculate() [12] void std::_Destroy_aux<true>::__destroy<int*>(int*, int*) [36] std::vector<double, std::allocator<double> >::vector()
 [14] __gnu_cxx::new_allocator<double>::deallocate(double*, unsigned long) [24] std::_Vector_base<double, std::allocator<double> >::_Vector_impl::_Vector_impl() [37] std::vector<double, std::allocator<double> >::~vector()
 [16] __gnu_cxx::new_allocator<double>::new_allocator() [25] std::_Vector_base<double, std::allocator<double> >::_Vector_impl::~_Vector_impl() [9] std::vector<double, std::allocator<double> >::operator[](unsigned long)
 [17] __gnu_cxx::new_allocator<double>::~new_allocator() [26] std::_Vector_base<double, std::allocator<double> >::_M_deallocate(double*, unsigned long) [44] std::vector<int, std::allocator<int> >::vector()
 [15] __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) [27] std::_Vector_base<double, std::allocator<double> >::_M_get_Tp_allocator() [45] std::vector<int, std::allocator<int> >::~vector()
 [18] __gnu_cxx::new_allocator<int>::new_allocator() [28] std::_Vector_base<double, std::allocator<double> >::_Vector_base() [11] void std::_Destroy<double*>(double*, double*)
 [19] __gnu_cxx::new_allocator<int>::~new_allocator() [29] std::_Vector_base<double, std::allocator<double> >::~_Vector_base() [38] void std::_Destroy<double*, double>(double*, double*, std::allocator<double>&)
  [8] std::vector<double, std::allocator<double> >::size() const [30] std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl() [13] void std::_Destroy<int*>(int*, int*)
 [20] std::allocator<double>::allocator() [31] std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl() [39] void std::_Destroy<int*, int>(int*, int*, std::allocator<int>&)
 [21] std::allocator<double>::~allocator() [32] std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long)

你可能感兴趣的:(linux,性能测试)