C语言中,我想显示程序运行时间,请问问题在哪?

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
int main()
{
time_t start,end;
start=time(NULL);
Sleep(10); //第一个字母S必须大写,在windows.h中定义
end=time(NULL);
printf("程序运行时间为:%f\n",difftime(end,start));
system("pause");
return 0;
}
输出的竟然是0.00000

修改如下,可以精确到毫秒的
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
int main()
{
time_t start,end;start=clock();//用clock(),它可以精确到毫秒的
Sleep(30); //调试的时候发现好像要大于10才能输出时间
end=clock();
printf("程序运行时间为:%lfs\n",double(end-start)/CLOCKS_PER_SEC);
//CLOCKS_PER_SEC在time.h中定义,为1000,为毫秒换算成秒的基
system("pause");
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-23
Sleep(10)是暂停10毫秒, difftime算出来的单位是秒,由于精度有限输出0.00000可以理解。楼主把Sleep(10)改为Sleep(1000)就可以看出效果。追问

那为什么不会输出0.01000呢,还是说最后的结果就只能是整数的秒,然后小数一定都是0呢

相似回答