itstx 于 2013.08.21 21:35 提问

gprof得到的结果中%time中的数值有问题

#include
#define NUM 10000
void bubble(int *a, int len) {

int i, j, t;
for(i = len - 1; i>0; i--) {
for(j = 0; j < i; j++)
if(a[j+1] < a[j]) {
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
void print(int *a, int len) {
int i;
for(i = 0; i < len; i++)
printf("%d ", a[i]);
printf("\n");
}
int main() {
int a[NUM];
int i;
for(i=0;i<NUM;i++)
a[i]=rand()%(NUM*100);
bubble(a, sizeof(a)/sizeof(int));
print(a, sizeof(a)/sizeof(int));
return 0;
}

gcc -o bubble bubble.c -pg
./bubble
gprof bubble gmon.out -b >result

Flat profile:

Each sample counts as 0.01 seconds.
% cumulative self self total

time seconds seconds calls ms/call ms/call name

100.41 0.67 0.67 1 672.73 672.73 bubble
0.00 0.67 0.00 1 0.00 0.00 print

Call graph

granularity: each sample hit covers 2 byte(s) for 1.49% of 0.67 seconds

index % time self children called name
0.67 0.00 1/1 main [2]

## [1] 100.0 0.67 0.00 1 bubble [1]

``````                                             <spontaneous>
``````

[2] 100.0 0.00 0.67 main [2]
0.67 0.00 1/1 bubble [1]

## 0.00 0.00 1/1 print [3]

``````            0.00    0.00       1/1           main [2]
``````

## [3] 0.0 0.00 0.00 1 print [3]

Index by function name

[1] bubble [3] print