gpt4 book ai didi

c - 执行时间可以显示

转载 作者:行者123 更新时间:2023-11-30 17:54:02 26 4
gpt4 key购买 nike

#include <conio.h>
#include <stdio.h>
#include<time.h>
double multi();

void main()
{
clrscr();
clock_t start = clock();
for (int i = 0; i < 1000; i++)
{
multi();
//printf("Answer (%d)",s);
}
clock_t end = clock();
float diff;
diff = (float) (end - start) / CLOCKS_PER_SEC;
printf("time execution :%f", diff);
getch();
}

double multi()
{
double a;
a = 5 * 5;
return a;
}

执行时间显示为0.000000什么问题!

这是否是纳秒的原因

最佳答案

clock() 的人函数说:

The clock() function returns an approximation of processor time used by the program.

近似值,因此不会很精确,这取决于系统的粒度。因此,对于初学者,您可以使用以下命令检查系统上 clock() 的粒度:

clock_t start =clock(), end;
while(1)
{
if(start != (end=clock()))
break;
}
diff=(float)(end - start)/CLOCKS_PER_SEC;
printf("best time :%f",diff);

为我这样做,我得到 0.001(即 1 毫秒),因此任何花费少于 1 毫秒的时间我都会返回“0”。这就是发生在你身上的事情,你的代码运行速度比时钟()的粒度更快,所以你得到了最好的近似值,恰好是“0”

关于c - 执行时间可以显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15280268/

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