gprof 的使用
gprof 已经安装在 NOI Linux 2.0 中,是 Linux 中一个使用的程序性能分析工具。欲将对程序 main.cpp
进行性能分析,需要在编译选项中加入 -pg
:
g++ main.cpp -o main -pg
然后正常运行程序:
./main
此时会生成一个 gmon.out
文件,存储了性能分析信息,需要使用 gprof 进行翻译,输出人类可读的信息(以下五选一):
gprof main gmon.out
gprof main # 这样也可以
gprof -b main # -b 不会输出每一项的英文解释(相当于简洁输出)
gprof main > report.txt # 建议输出到文件再查看
gprof main | view - # 或者你比较极端也可以
建议使用 vim 查看 gprof 的信息,因为 vim 有对它的高亮。程序调用树中,紫色行即为当前描述的函数,紫色行上方是它的父级函数,下方是它所调用的函数。或者也可以看 index 列,左边写了 index 的是当前描述的函数。
gprof 信息的中文翻译
- %time: 此函数使用的程序总运行时间的百分比。
- cumulative seconds: 此函数所占秒数与上面列出的秒数的总和。
- self seconds: 仅此函数所占的秒数。这是列表排序的第一关键字。
- calls: 调用此函数的次数。
- self ms/call: 每次调用此函数所花费的平均毫秒数。
- total ms/call: 每次调用此函数及其后代所花费的平均毫秒数。
- 以上,如果该函数没有被分析,那么对应项为空。
此表描述了程序的调用树,并按每个函数及其子函数所花费的总时间进行排序。此表中的每个条目由几行组成。左侧空白处的索引编号行列出了当前函数。上面的几行列出了调用此函数的函数,下面的几行则列出了此函数调用的函数。
此行列出了:
- index: 表中每个元素的唯一编号。索引号按数字排序。索引号印在每个函数名称旁边,这样更容易在表中查找函数的位置。
- %time: 这是用于此功能及其子功能的“总”时间的百分比。请注意,由于不同的观点、选项排除的功能等,这些数字加起来不会达到 100%。
- self: 这是在此函数中花费的总时间。
- children: 这是子函数传播到此函数中的总时间。
- called: 这是函数被调用的次数。如果函数递归调用自身,则该数字仅包括非递归调用,后面是“+”和递归调用的数量。
- name: 当前函数的名称。索引号打印在其后。如果函数是循环(cycle,下文有解释)的成员,则循环号打印在函数名称和索引号之间。
对于函数的父亲(即调用它的函数),字段具有以下含义:
- self: 这是从函数直接传播到此父级的时间量。(was propagated,可以理解为在……中所占的时间)
- children: 这是从函数的子级传播到此父级的时间量。
- called: 这是父级调用函数的次数 "/" 函数被调用的总次数。对函数的递归调用不包含在“/”后面的数字中。
- name: 这是父级的名字。父级的索引号将打印在其后。如果父级是循环的成员,则循环号将打印在名称和索引号之间。如果无法确定函数的父级,则会在“name”字段中打印“<spontaneous>”一词,而所有其他字段都是空的。
对于函数的孩子(即它调用的函数),字段具有以下含义:
- self: 这是从孩子直接传播到函数中的时间量。
- children: 这是从孩子的孩子传播到函数的时间量。
- called: 这是函数调用此子项的次数 “/” 子项被调用的总次数。孩子的递归调用不会包含在“/”后面的数字中。
- name: 这是孩子的名字。子项的索引号将打印在其后。如果子项是循环的成员,则循环号将打印在名称和索引号之间。
如果调用图中有任何循环(圆圈,cycles & circles),则整个循环都有一个条目。此条目显示谁调用了循环(作为父项)和循环的成员(作为子项)。“+”递归调用条目显示了循环内部的函数调用次数,每个成员的调用条目显示该成员从循环的其他成员调用的次数。
示例
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls ms/call ms/call name
53.66 0.22 0.22 1599992 0.00 0.00 chkmax(std::vector<long long, std::allocator<long long> >&, minkowski_t<long long>, int)
26.83 0.33 0.11 29033026 0.00 0.00 minkowski_t<long long>::next()
7.32 0.36 0.03 1199994 0.00 0.00 bitmax(std::vector<long long, std::allocator<long long> > const&, std::vector<long long, std::allocator<long long> > const&, int)
7.32 0.39 0.03 1 30.00 410.00 solve(int, int)
2.44 0.40 0.01 2399988 0.00 0.00 chkmaxp(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, long long)
2.44 0.41 0.01 799996 0.00 0.00 chkmax(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, int)
0.00 0.41 0.00 799996 0.00 0.00 std::vector<long long, std::allocator<long long> >::_M_default_append(unsigned long)
0.00 0.41 0.00 600000 0.00 0.00 std::vector<long long, std::allocator<long long> >::operator=(std::vector<long long, std::allocator<long long> > const&)
% 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.
total the average number of milliseconds spent in this
ms/call function and its descendents per call, if this
function is profiled, else blank.
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.
Copyright (C) 2012-2022 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Call graph (explanation follows)
granularity: each sample hit covers 4 byte(s) for 2.44% of 0.41 seconds
index % time self children called name
<spontaneous>
[1] 100.0 0.00 0.41 main [1]
0.03 0.38 1/1 solve(int, int) [2]
-----------------------------------------------
399998 solve(int, int) [2]
0.03 0.38 1/1 main [1]
[2] 100.0 0.03 0.38 1+399998 solve(int, int) [2]
0.22 0.00 1599992/1599992 chkmax(std::vector<long long, std::allocator<long long> >&, minkowski_t<long long>, int) [3]
0.11 0.00 29033026/29033026 minkowski_t<long long>::next() [4]
0.03 0.00 1199994/1199994 bitmax(std::vector<long long, std::allocator<long long> > const&, std::vector<long long, std::allocator<long long> > const&, int) [5]
0.01 0.00 2399988/2399988 chkmaxp(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, long long) [6]
0.01 0.00 799996/799996 chkmax(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, int) [7]
0.00 0.00 799996/799996 std::vector<long long, std::allocator<long long> >::_M_default_append(unsigned long) [14]
0.00 0.00 600000/600000 std::vector<long long, std::allocator<long long> >::operator=(std::vector<long long, std::allocator<long long> > const&) [15]
399998 solve(int, int) [2]
-----------------------------------------------
0.22 0.00 1599992/1599992 solve(int, int) [2]
[3] 53.7 0.22 0.00 1599992 chkmax(std::vector<long long, std::allocator<long long> >&, minkowski_t<long long>, int) [3]
-----------------------------------------------
0.11 0.00 29033026/29033026 solve(int, int) [2]
[4] 26.8 0.11 0.00 29033026 minkowski_t<long long>::next() [4]
-----------------------------------------------
0.03 0.00 1199994/1199994 solve(int, int) [2]
[5] 7.3 0.03 0.00 1199994 bitmax(std::vector<long long, std::allocator<long long> > const&, std::vector<long long, std::allocator<long long> > const&, int) [5]
-----------------------------------------------
0.01 0.00 2399988/2399988 solve(int, int) [2]
[6] 2.4 0.01 0.00 2399988 chkmaxp(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, long long) [6]
-----------------------------------------------
0.01 0.00 799996/799996 solve(int, int) [2]
[7] 2.4 0.01 0.00 799996 chkmax(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, int) [7]
-----------------------------------------------
0.00 0.00 799996/799996 solve(int, int) [2]
[14] 0.0 0.00 0.00 799996 std::vector<long long, std::allocator<long long> >::_M_default_append(unsigned long) [14]
-----------------------------------------------
0.00 0.00 600000/600000 solve(int, int) [2]
[15] 0.0 0.00 0.00 600000 std::vector<long long, std::allocator<long long> >::operator=(std::vector<long long, std::allocator<long long> > const&) [15]
-----------------------------------------------
This table describes the call tree of the program, and was sorted by
the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the
index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
and the lines below it list the functions this one called.
This line lists:
index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name so
it is easier to look up where the function is in the table.
% time This is the percentage of the `total' time that was spent
in this function and its children. Note that due to
different viewpoints, functions excluded by options, etc,
these numbers will NOT add up to 100%.
self This is the total amount of time spent in this function.
children This is the total amount of time propagated into this
function by its children.
called This is the number of times the function was called.
If the function called itself recursively, the number
only includes non-recursive calls, and is followed by
a `+' and the number of recursive calls.
name The name of the current function. The index number is
printed after it. If the function is a member of a
cycle, the cycle number is printed between the
function's name and the index number.
For the function's parents, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the function into this parent.
children This is the amount of time that was propagated from
the function's children into this parent.
called This is the number of times this parent called the
function `/' the total number of times the function
was called. Recursive calls to the function are not
included in the number after the `/'.
name This is the name of the parent. The parent's index
number is printed after it. If the parent is a
member of a cycle, the cycle number is printed between
the name and the index number.
If the parents of the function cannot be determined, the word
`<spontaneous>' is printed in the `name' field, and all the other
fields are blank.
For the function's children, the fields have the following meanings:
self This is the amount of time that was propagated directly
from the child into the function.
children This is the amount of time that was propagated from the
child's children to the function.
called This is the number of times the function called
this child `/' the total number of times the child
was called. Recursive calls by the child are not
listed in the number after the `/'.
name This is the name of the child. The child's index
number is printed after it. If the child is a
member of a cycle, the cycle number is printed
between the name and the index number.
If there are any cycles (circles) in the call graph, there is an
entry for the cycle-as-a-whole. This entry shows who called the
cycle (as parents) and the members of the cycle (as children.)
The `+' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry for each member shows,
for that member, how many times it was called from other members of
the cycle.
Copyright (C) 2012-2022 Free Software Foundation, Inc.
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
Index by function name
[2] solve(int, int) [7] chkmax(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, int) [14] std::vector<long long, std::allocator<long long> >::_M_default_append(unsigned long)
[5] bitmax(std::vector<long long, std::allocator<long long> > const&, std::vector<long long, std::allocator<long long> > const&, int) [6] chkmaxp(std::vector<long long, std::allocator<long long> >&, std::vector<long long, std::allocator<long long> > const&, long long) [15] std::vector<long long, std::allocator<long long> >::operator=(std::vector<long long, std::allocator<long long> > const&)
[3] chkmax(std::vector<long long, std::allocator<long long> >&, minkowski_t<long long>, int) [4] minkowski_t<long long>::next()
你可以看一下能不能用上面的中文翻译大致看懂。
标签:std,function,中文翻译,int,0.00,gprof,number,vector,Linux From: https://www.cnblogs.com/caijianhong/p/18408481