0
votes

I am having the following program

#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);
    }

}

I tried to profile my above program.

I have compiled the program as follows

cc -pg linklistadv.c

Then I ran the program as follows

./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

Now I used the gprof command as follows gprof -p a.out gmon.out

But it is showing the output as follows

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.

What is the problem? Why it is showing 0.00 as the time accounted? The display function will run for 100 seconds. But it is showing the time as 0.00 only. Please guide me.

Thanks in Advance!

1
Before dumping that much code, most of which is (probably) irrelevant to your problem at hand, try whittling it down to the simplest example that produces the same error. In doing so ou may solve your own problem. - Chris Lutz

1 Answers

2
votes

sleep(100) does not use 100 seconds of CPU time. Instead, the OS's kernel suspends the program execution. gprof measures only the used CPU time, not the time the program was suspended. You might want to search for explanations of "CPU time" and "wallclock time".