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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
#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;
}
Run Code Online (Sandbox Code Playgroud)
CLOCKS_PER_SEC 是否因系统而异,或者它对于操作系统是恒定的还是依赖于该特定系统的处理器?
CLOCKS_PER_SEC最终由编译器及其标准库实现决定,而不是由操作系统决定。尽管机器、操作系统和其他因素会影响编译器提供的内容。
帮我解释一下我的代码的输出……是吗?
号printf("\nTotal time:%u",(double)(end - start)/CLOCKS_PER_SEC);使用"%u"打印double。@菲利克斯帕尔门
CLOCKS_PER_SEC不一定是unsigned.
clock_t不一定是int. Ref
使用错误的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);
Run Code Online (Sandbox Code Playgroud)
甚至考虑long double。
long double t = (long double)(end - start)/CLOCKS_PER_SEC;
printf("Total time:%Lg\n", t);
Run Code Online (Sandbox Code Playgroud)