【发布时间】:2018-07-24 14:39:23
【问题描述】:
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.
【问题讨论】:
-
您认为为什么将其定义为宏?当然,你不应该假设它在每个系统上都是一样的!
-
AFAIK,POSIX 将其定义为 1'000'000。所以你似乎在一个非 POSIX 系统上。
-
顺便说一句,
%u是用于打印double的错误格式说明符。