gpt4 book ai didi

c - gprof 中的问题(始终记为零)

转载 作者:行者123 更新时间:2023-11-30 14:29:15 27 4
gpt4 key购买 nike

我有以下程序

#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。请指导我。

提前致谢!

最佳答案

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

关于c - gprof 中的问题(始终记为零),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5048925/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com