给出以下代码,调用f或g
#include <stdlib.h>
#include <stdio.h>
int f() {
return 0;
}
int g() {
return 1;
}
int main() {
long sum = 0;
for(int i = 0; i < 1000*1000*1000; i++) {
int result;
if(rand() % 2 == 0) {
result = f();
}
else {
result = g();
}
sum += result;
}
printf("%ld\n", sum);
}
我用
编译g++ test.c -o doom -lprofiler -lunwind
用
运行CPUPROFILE=./test.txt ./test
然后使用
生成一个gifpprof --gif ./test ./test.txt > output.gif
我得到以下
但是,我的边缘从f到g,f到自身,g到自身。没有优化(我再次尝试使用-O0),我也尝试过使用和不使用-lunwind。
为什么分析器会认为f调用g(和它自己的somtimes)?如何正确使用分析器?