问题gprof的(总是占为零)

问题描述:

我有以下程序问题gprof的(总是占为零)

#include<stdio.h> 
#include<stdlib.h> 
typedef struct struct_node 
{ 
     int data; 
     struct struct_node *next; 
}node; 
typedef node* list; 
list st=NULL; 
list prev; 
list insert(list); 
list delete(list); 
void display(list); 
int n; 
main() 
{ 
    int ch; 
    while(1) 
    { 
     printf("MENU\n"); 
     printf("----\n"); 
     printf("1.INSERT\n"); 
     printf("2.DELETE\n"); 
     printf("3.PRINT\n"); 
     printf("4.EXIT\n"); 
     printf("ENTER YOUR CHOICE:"); 
     scanf("%d",&ch); 
     switch(ch) 
     { 
      case 1: 
       st=insert(st); 
       printf("NEW NODE INSERTED SUCCESSFULLY\n"); 
       break; 
      case 2: 
       st=delete(st); 
       printf("\nDELETION SUCCESSFUL.CONTENTS ARE:\n"); 
       display(st); 
       break; 
      case 3: 
       display(st); 
       break; 
      case 4: 
       exit(0); 
      default: 
       printf("IT IS INVALID CHOICE\n"); 
       exit(0); 
     } 
    } 
} 
void display(list temp) 
{ 

    sleep(100); 
    printf("THE LINKED LIST\n"); 
    printf("---------------\n"); 
    while(temp) 
    { 
     printf("%d\n",temp->data); 
     temp=temp->next; 
    } 

} 
list insert(list temp) 
{ 
    list new; 
    int i=0; 
    int first=0; 
    int ch; 
    list prev,current; 
    printf("ENTER THE DATA TO INSERT:"); 
    scanf("%d",&n); 
    printf("MENU\n"); 
    printf("----\n"); 
    printf("1.INSERT AT FIRST\n"); 
    printf("2.INSERT AT LAST \n"); 
    printf("3.INSERT AT THE MIDDLE\n"); 
    printf("ENTER YOUR CHOICE:"); 
    scanf("%d",&ch); 
    switch(ch) 
    { 
      case 1: 
      new=(list)malloc(sizeof(list)); 
      new->data=n;  
      new->next=temp; 
      st=new; 
      return st; 
      break; 
     case 2: 
      st=temp; 
      while(temp->next!=NULL) 
       temp=temp->next; 
      new=(list)malloc(sizeof(list)); 
      temp->next=new; 
      new->data=n; 
      new->next=NULL; 
      return st; 
      break; 
     case 3: 
      new=(list)malloc(sizeof(list)); 
       new->data=n; 
      st=temp; 
      while(temp->next!=NULL) 
      { 
       ++i; 
       prev=temp; 
       if(prev->data < n) 
       { 
        ++first; 
        current=prev->next; 
        if(current->data > n) 
        { 
         new->next=current; 
         prev->next=new; 
         return st; 
        } 
       } 
       temp=temp->next; 
      } 
      if(i==0) 
      { 
       if(temp->data < n) 
       { 
        temp->next=new; 
        new->data=n; 
        new->next=NULL; 
       } 
       else 
       { 
        new->next=temp; 
        temp->next=NULL; 
        st=new; 
       } 
      } 
      else 
      { 
       if(first==0) 
       { 
        new->next=temp; 
        new->data=n; 
        return st; 
       } 
       temp->next=new; 
       new->data=n; 
       new->next=NULL; 
      }      
      return st; 
      break; 
      default: 
       printf("INVALID CHOICE\n"); 
       break; 
       }       
} 
list delete(list temp) 
{ 
    int ch; 
    int val; 
    display(st); 
    printf("MENU\n"); 
    printf("1.DELETE THE FROM FIRST\n"); 
    printf("2.DELETE FROM LAST NODE\n"); 
    printf("3.DELETE FROM THE MIDDLE\n"); 
    printf("ENTER YOUR OPTION:"); 
    scanf("%d",&ch); 
    switch(ch) 
    { 
     case 1: 
      temp=temp->next; 
      st=temp; 
      return st; 
      break; 
     case 2: 
      st=temp; 
      prev=st; 
      while(temp->next!=NULL) 
      { 
       prev=temp; 
       temp=temp->next; 
      } 
      prev->next=NULL; 
      return st; 
      break; 
     case 3: 
      st=temp; 
      prev=st; 
        printf("ENTER THE DATA TO BE REMOVED\n"); 
      scanf("%d",&val); 
      while(temp->data!=val) 
      { 
       prev=temp; 
       temp=temp->next; 
      } 
      prev->next=temp->next; 
      return st; 
      break; 
     default: 
      printf("INVALID CHOICE\n"); 
      exit(0); 
    } 

} 

我试图分析我上面的程序。

如下

我已编译的程序CC -pg linklistadv.c

然后我跑的程序如下

./a.out 
MENU 
---- 
1.INSERT 
2.DELETE 
3.PRINT 
4.EXIT 
ENTER YOUR CHOICE:1 
ENTER THE DATA TO INSERT:1 
MENU 
---- 
1.INSERT AT FIRST 
2.INSERT AT LAST 
3.INSERT AT THE MIDDLE 
ENTER YOUR CHOICE:1 
NEW NODE INSERTED SUCCESSFULLY 
MENU 
---- 
1.INSERT 
2.DELETE 
3.PRINT 
4.EXIT 
ENTER YOUR CHOICE:3 
THE LINKED LIST 
--------------- 
1 
MENU 
---- 
1.INSERT 
2.DELETE 
3.PRINT 
4.EXIT 
ENTER YOUR CHOICE:4 

现在我用gprof命令如下 gprof -p a.out gmon.out

但它显示如下输出

Flat profile: 

Each sample counts as 0.01 seconds. 
no time accumulated 

    % cumulative self    self  total   
time seconds seconds calls Ts/call Ts/call name  
    0.00  0.00  0.00  1  0.00  0.00 display 
    0.00  0.00  0.00  1  0.00  0.00 insert 

%   the percentage of the total running time of the 
time  program used by this function. 

cumulative a running sum of the number of seconds accounted 
seconds for by this function and those listed above it. 

self  the number of seconds accounted for by this 
seconds function alone. This is the major sort for this 
      listing. 

calls  the number of times this function was invoked, if 
      this function is profiled, else blank. 

self  the average number of milliseconds spent in this 
ms/call function per call, if this function is profiled, 
     else blank. 

total  the average number of milliseconds spent in this 
ms/call function and its descendents per call, if this 
     function is profiled, else blank. 

name  the name of the function. This is the minor sort 
      for this listing. The index shows the location of 
     the function in the gprof listing. If the index is 
     in parenthesis it shows where it would appear in 
     the gprof listing if it were to be printed. 

什么问题? 为什么它显示0.00作为时间? 显示功能将运行100秒。 但它只显示0.00的时间。 请指导我。

在此先感谢!

+0

在转储那么多代码之前,其中大部分代码(可能)与您手头的问题无关,请尽量减少产生相同错误的最简单示例。这样做可以解决你自己的问题。 – 2011-02-19 06:21:31

睡眠(100)不使用100秒的CPU时间。相反,操作系统的内核暂停程序执行。 gprof仅测量使用的 CPU时间,而不是程序被暂停的时间。您可能想要搜索“CPU时间”和“挂钟时间”的解释。

+0

感谢您的回答......告诉我在上述程序的哪种情况下,可以在配置文件输出中显示0.00以上的秒数? – thillaiselvan 2011-02-19 06:53:00