问题描述
我有这个程序,需要运行2.34秒,而gprof表示只需要1.18秒.我在其他地方读过答案,暗示如果程序受I/O约束,则gprof可能会出错,但是此程序显然不是.
I have this program that takes 2.34 seconds to run, and gprof says it only takes 1.18 seconds. I've read answers elsewhere suggesting that gprof can get it wrong if e.g the program is I/O bound, but this program clearly isn't.
对于我正在尝试分析的有用程序,也会发生这种情况.这并非特定于这个琐碎的测试用例.
This also happens for a useful program I'm trying to profile. It's not specific to this trivial test case.
(在这种情况下,gprof还说main()占用了程序运行时间的100%以上,这是一个非常愚蠢的错误,但并没有真正给我造成问题.)
(Also in this case gprof says that main() takes more than 100% of the program's running time, which is a pretty stupid bug but not really causing problems for me.)
$ 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.
推荐答案
我建议删除gprof
并切换到oprofile
.任何将检测插入程序的配置文件都会从本质上影响性能,从而可能会使结果产生偏差或使结果无效.使用oprofile
,您不必借助概要分析支持来构建程序,也不必获得启用特殊概要分析的库.它通过统计方法工作,基本上是在内核协助下对指令指针进行采样,并使用样本计数来估算每个函数花费了多少时间.
I would recommend dropping gprof
and switching to oprofile
. Any profiling that inserts instrumentation into your program will inherently affect the performance in ways that might skew or invalidate the results. With oprofile
you don't have to build your program with profiling support or get special profiling-enabled libraries; it works by statistical methods, basically sampling the instruction pointer (with kernel assistance) and using the sample counts to estimate how much time was spent in each function.
这篇关于为什么gprof大大低估了程序的运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!