我有这个程序需要 2.34 秒才能运行,而 gprof 说它只需要 1.18 秒。我在其他地方读过的答案表明,如果程序是 I/O 绑定的,gprof 可能会出错,但这个程序显然不是。
这也发生在我试图分析的一个有用的程序上。它并不特定于这个琐碎的测试用例。
(同样在这种情况下,gprof 说 main() 占用了程序运行时间的 100% 以上,这是一个非常愚蠢的错误,但对我来说并没有真正引起问题。)
$ cat test.c
int main() {
int i;
for (i=0;i<1000000000;i++);
}
$ gcc test.c -o test
$ time ./test
real 0m2.342s
user 0m2.340s
sys 0m0.000s
$ gcc test.c -o test -pg
$ time ./test
real 0m2.342s
user 0m2.340s
sys 0m0.000s
$ gprof test |head
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
101.33 1.18 1.18 main
% the percentage of the total running time of the
time program used by this function.