C++软件开发常用辅助软件——gprof

gprof

生成函数调用关系以及调用次数,并统计耗时。用于优化代码或发现隐藏的问题。

示例

示例代码

 1 #include <stdio.h>
 2 
 3 void FunB()
 4 {
 5     int a = 1;
 6     int b = 2;
 7     int c;
 8     for(int i = 0; i < 100000000; ++i) {
 9         c = a / b;
10     }
11 }
12 
13 void FunA()
14 {
15     int a = 1;
16     int b = 2;
17     int c;
18     for(int i = 0; i < 100000000; ++i) {
19         c = a + b;
20     }
21     FunB();
22 }
23 
24 int main()
25 {
26     FunA();
27     return 0;
28 }
View Code

编译程序(编译选项/链接选项都要添加”-pg“)

g++ -o a.out test.cpp -pg

运行程序

./a.out

分析程序

gprof a.out gmon.out  > report

其中gmon.out为运行程序后生成的辅助文件(注意:程序需要正常退出结束),打开report文件

  1 Flat profile:
  2 
  3 Each sample counts as 0.01 seconds.
  4   %   cumulative   self              self     total           
  5  time   seconds   seconds    calls  ms/call  ms/call  name    
  6  68.97      0.23     0.23        1   234.51   234.51  FunB()
  7  26.99      0.33     0.09        1    91.76   326.27  FunA()
  8 
  9  %         the percentage of the total running time of the
 10 time       program used by this function.
 11 
 12 cumulative a running sum of the number of seconds accounted
 13  seconds   for by this function and those listed above it.
 14 
 15  self      the number of seconds accounted for by this
 16 seconds    function alone.  This is the major sort for this
 17            listing.
 18 
 19 calls      the number of times this function was invoked, if
 20            this function is profiled, else blank.
 21 
 22  self      the average number of milliseconds spent in this
 23 ms/call    function per call, if this function is profiled,
 24            else blank.
 25 
 26  total     the average number of milliseconds spent in this
 27 ms/call    function and its descendents per call, if this
 28            function is profiled, else blank.
 29 
 30 name       the name of the function.  This is the minor sort
 31            for this listing. The index shows the location of
 32            the function in the gprof listing. If the index is
 33            in parenthesis it shows where it would appear in
 34            the gprof listing if it were to be printed.
 35 
 36 
 37 Copyright (C) 2012-2019 Free Software Foundation, Inc.
 38 
 39 Copying and distribution of this file, with or without modification,
 40 are permitted in any medium without royalty provided the copyright
 41 notice and this notice are preserved.
 42 
 43 
 44                      Call graph (explanation follows)
 45 
 46 
 47 granularity: each sample hit covers 2 byte(s) for 3.06% of 0.33 seconds
 48 
 49 index % time    self  children    called     name
 50                                                  <spontaneous>
 51 [1]    100.0    0.00    0.33                 main [1]
 52                 0.09    0.23       1/1           FunA() [2]
 53 -----------------------------------------------
 54                 0.09    0.23       1/1           main [1]
 55 [2]    100.0    0.09    0.23       1         FunA() [2]
 56                 0.23    0.00       1/1           FunB() [3]
 57 -----------------------------------------------
 58                 0.23    0.00       1/1           FunA() [2]
 59 [3]     71.9    0.23    0.00       1         FunB() [3]
 60 -----------------------------------------------
 61 
 62  This table describes the call tree of the program, and was sorted by
 63  the total amount of time spent in each function and its children.
 64 
 65  Each entry in this table consists of several lines.  The line with the
 66  index number at the left hand margin lists the current function.
 67  The lines above it list the functions that called this function,
 68  and the lines below it list the functions this one called.
 69  This line lists:
 70      index      A unique number given to each element of the table.
 71                 Index numbers are sorted numerically.
 72                 The index number is printed next to every function name so
 73                 it is easier to look up where the function is in the table.
 74 
 75      % time     This is the percentage of the `total' time that was spent
 76                 in this function and its children.  Note that due to
 77                 different viewpoints, functions excluded by options, etc,
 78                 these numbers will NOT add up to 100%.
 79 
 80      self       This is the total amount of time spent in this function.
 81 
 82      children   This is the total amount of time propagated into this
 83                 function by its children.
 84 
 85      called     This is the number of times the function was called.
 86                 If the function called itself recursively, the number
 87                 only includes non-recursive calls, and is followed by
 88                 a `+' and the number of recursive calls.
 89 
 90      name       The name of the current function.  The index number is
 91                 printed after it.  If the function is a member of a
 92                 cycle, the cycle number is printed between the
 93                 function's name and the index number.
 94 
 95 
 96  For the function's parents, the fields have the following meanings:
 97 
 98      self       This is the amount of time that was propagated directly
 99                 from the function into this parent.
100 
101      children   This is the amount of time that was propagated from
102                 the function's children into this parent.
103 
104      called     This is the number of times this parent called the
105                 function `/' the total number of times the function
106                 was called.  Recursive calls to the function are not
107                 included in the number after the `/'.
108 
109      name       This is the name of the parent.  The parent's index
110                 number is printed after it.  If the parent is a
111                 member of a cycle, the cycle number is printed between
112                 the name and the index number.
113 
114  If the parents of the function cannot be determined, the word
115  `<spontaneous>' is printed in the `name' field, and all the other
116  fields are blank.
117 
118  For the function's children, the fields have the following meanings:
119 
120      self       This is the amount of time that was propagated directly
121                 from the child into the function.
122 
123      children   This is the amount of time that was propagated from the
124                 child's children to the function.
125 
126      called     This is the number of times the function called
127                 this child `/' the total number of times the child
128                 was called.  Recursive calls by the child are not
129                 listed in the number after the `/'.
130 
131      name       This is the name of the child.  The child's index
132                 number is printed after it.  If the child is a
133                 member of a cycle, the cycle number is printed
134                 between the name and the index number.
135 
136  If there are any cycles (circles) in the call graph, there is an
137  entry for the cycle-as-a-whole.  This entry shows who called the
138  cycle (as parents) and the members of the cycle (as children.)
139  The `+' recursive calls entry shows the number of function calls that
140  were internal to the cycle, and the calls entry for each member shows,
141  for that member, how many times it was called from other members of
142  the cycle.
143 
144 
145 Copyright (C) 2012-2019 Free Software Foundation, Inc.
146 
147 Copying and distribution of this file, with or without modification,
148 are permitted in any medium without royalty provided the copyright
149 notice and this notice are preserved.
150 
151 
152 Index by function name
153 
154    [2] FunA()                  [3] FunB()
View Code
原文地址:https://www.cnblogs.com/NoSoul/p/13357001.html