gpt4 book ai didi

c - 为什么 gprof 明显低估了程序的运行时间?

转载 作者:太空狗 更新时间:2023-10-29 15:01:24 25 4
gpt4 key购买 nike

我的这个程序需要 2.34 秒才能运行,而 gprof 说它只需要 1.18 秒。我在其他地方读到的答案表明,如果程序是 I/O 绑定(bind)的,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.

最佳答案

我建议放弃 gprof 并切换到 oprofile。任何将检测插入程序的分析都将以可能扭曲或使结果无效的方式固有地影响性能。使用 oprofile,您不必构建具有分析支持的程序或获得特殊的支持分析的库;它通过统计方法工作,基本上是对指令指针进行采样(在内核协助下)并使用样本计数来估计每个函数花费了多少时间。

关于c - 为什么 gprof 明显低估了程序的运行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4309831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com