gpt4 book ai didi

C语言中的CLOCKS_PER_SEC找到time.h库

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:55 25 4
gpt4 key购买 nike

CLOCKS_PER_SEC 是因系统而异,还是对于操作系统而言是恒定的,还是取决于该特定系统的处理器?还要帮我解释代码的输出...对吗??

#include<stdio.h>
#include<time.h>
int main()
{
int a;
long int b;
clock_t start, end;

start = clock();

//Code for which the time is to be calculated
for(a=0;;a++)
{
if(a<0)
{
break;
}
}
printf("int : %u\n",a);
for(b=0;;b++)
{
if(b<0)
{
break;
}
}
printf("long int :%u\n",b);
//code is over

end = clock();

//CLOCKS_PER_SECOND : the number of clock ticks per second
printf("Starting Time:%u\n",start);
printf("Ending Time:%u\n",end);
printf("CLOCKS_PER_SEC:%u",CLOCKS_PER_SEC);
printf("\nNumber of clock ticks:%u",(end - start));
printf("\nTotal time:%u",(double)(end - start)/CLOCKS_PER_SEC);
return 0;
}

输出:

int : 2147483648
long int :2147483648
Starting Time:0
Ending Time:9073
CLOCKS_PER_SEC:1000
Number of clock ticks:9073
Total time:1099511628
Process returned 0 (0x0) execution time : 15.653 s
Press any key to continue.

最佳答案

Does CLOCKS_PER_SEC varies from system to system or is it constant for an operating system or is it dependent on the processor of that particular system?

CLOCKS_PER_SEC 最终由编译器及其标准库实现决定,而不是由操作系统决定。尽管机器、操作系统和其他因素会影响编译器提供的内容。

help me explain the output of my code...is it right?

没有。 printf("\nTotal time:%u",(double)(end - start)/CLOCKS_PER_SEC); 正在使用 "%u" 打印一个 double @Felix Palmen

CLOCKS_PER_SEC 不一定是unsigned
clock_t 不一定是 intRef
使用错误的 printf() 说明符会导致输出信息不足。
提示:启用所有编译器警告。

转换为宽类型并使用匹配的打印说明符。

clock_t start
// printf("Starting Time:%u\n",start);
printf("Starting Time:%g\n", (double) start);

// printf("CLOCKS_PER_SEC:%u",CLOCKS_PER_SEC);
printf("CLOCKS_PER_SEC:%g\n", (double) CLOCKS_PER_SEC);

// printf("\nTotal time:%u",(double)(end - start)/CLOCKS_PER_SEC);
printf("Total time:%g\n",(double)(end - start)/CLOCKS_PER_SEC);

或者甚至考虑long double

long double t = (long double)(end - start)/CLOCKS_PER_SEC;
printf("Total time:%Lg\n", t);

关于C语言中的CLOCKS_PER_SEC找到time.h库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51501410/

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