【发布时间】:2013-12-16 10:50:34
【问题描述】:
这是我从个人资料数据中得到的。我不明白为什么 main 调用不止一次?
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 96 0.00 0.00 fun
0.00 0.00 0.00 33 0.00 0.00 __x86.get_pc_thunk.bx
0.00 0.00 0.00 27 0.00 0.00 main
更奇怪的是,我的代码是这样的
void foo() {
foo1();
if ( a condition) foo2();
}
% cumulative self self total
time seconds seconds calls us/call us/call name
54.55 0.06 0.06 6115 9.81 9.81 foo1
18.18 0.08 0.02 252520 0.08 0.08 cmp_by_weight
9.09 0.09 0.01 865699 0.01 0.01 foo2
为什么 foo2() 的调用次数比 foo1() 的调用次数多?
我在编译时使用了 -pg 选项。然后使用 gprof -a exe gmon.out 从 gmon.out 生成配置文件。
Calls:是函数被调用的总次数。如果函数从未被调用,或者无法确定调用次数(可能是因为函数未在启用分析的情况下编译),则调用字段为空。
【问题讨论】:
-
很奇怪。 exe 和 gmon 文件之间可能存在一些 incostintencies。尝试删除这两个文件并重新构建。
-
@egur 我已经这样做了,但是得到了相同的结果。
-
你能发布完整的src代码吗?你的程序是 fork() 吗?
-
@tristan 完整的代码有点长。但我保证它是一个单线程程序并且没有fork()。