clock()!
书上说:clock()返回从程序开始执行起过去的时间,单位为1/100秒,于是我写了如下程序,旨在让我的程序停顿5秒钟,可结果没有那么长时间,不知道是何原因?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
clock_t later, early;
early = clock();
while(1)
{
;
later = clock();
if (later - early >= 500)
break;
}
system("pause");
return 0;
}
问题点数:10、回复次数:5Top
1 楼pottichu(拉拉是头猪)回复于 2006-07-04 19:50:43 得分 4
单位应该是1/1000秒吧!
if (later - early >= 5000)
就行了。Top
2 楼haha168_2002(啥时候我能成为高手啊?)回复于 2006-07-04 20:02:05 得分 0
我也是觉得应该1/1000,肯定吗???Top
3 楼mmmcd(超超)回复于 2006-07-04 21:38:40 得分 2
肯定Top
4 楼yingge(...木脑壳...)回复于 2006-07-04 21:58:23 得分 2
为什么我看到的man页不是这样说的呢
DESCRIPTION
The clock() function returns an approximation of processor time used by
the program.
RETURN VALUE
The value returned is the CPU time used so far as a clock_t; to get the
number of seconds used, divide by CLOCKS_PER_SEC. If the processor
time used is not available or its value cannot be represented, the
function returns the value (clock_t)-1.
楼主还是去查查time.h里CLOCKS_PER_SEC是怎么定义的吧
Top
5 楼wenysongbaobao(寻找腾格里的蘑菇)回复于 2006-07-04 22:48:20 得分 2
输出了一下CLOCKS_PER_SEC(VC6.0),是1000
不过我觉得保险的用法是if ((later - early) / CLOCKS_PER_SEC >= 5)
这样就肯定是5s了
Top




