Why does gprof significantly underestimate program runtime?

I have this program that takes 2.34 seconds to run, and gprof says it only takes 1.18 seconds. I read the answers elsewhere, suggesting that gprof might be wrong if, for example, a program is related to I / O, but this program is clearly not.

This also happens for a useful program that I am trying to profile. This does not apply to this trivial test case.

(Also in this case, gprof says that main () takes up more than 100% of the program’s time, which is a pretty stupid mistake, but it doesn't cause me problems.)

$ 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.
+5
source share
3 answers

gprof oprofile. , , , . oprofile ; , ( ) , , .

+3

-, , gprof , , - , . , - -, .

gprof . .

+2

, , 2.3 , . , .. ...

, ( ) ( ). , gmon.out, .. , . , . , , 2,34 - 1,18 = 1,16 ( ). gprof , gmon.out. , , gprof.

, gprof . 1/100 . 0,01 , . 0,01 , , 100%. , , , , , , , ( ). , gprof, , , , , 101,33%.

, . .

!

+1
source

All Articles