CLK_TCK
应用一
TC2.0中time.h中的CLK_TCK
#ifndef __CLOCK_T
#define __CLOCK_T
typedef long clock_t;
#define CLK_TCK 18.2
#endif
可见其值为18.2。
实际上在VC6.0中也有关于CLK_TCK的说明:
#if !STDC || defined(POSIX)
#define CLK_TCK CLOCKS_PER_SEC
_CRTIMP extern int daylight;
_CRTIMP extern long timezone;
_CRTIMP extern char * tzname[2];
_CRTIMP void __cdecl tzset(void);
#endif
应用二
VC++6.0中类似的CLOCKS_PER_SEC
在VC++6.0中类似的有CLOCKS_PER_SEC 。其值为1000。
#define CLOCKS_PER_SEC 1000
因此VC6.0中CLK_TCK的值不再是18.2,而是1000。
程序举例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void )
{
long i=10000000L;
clock_t start, finish;
double duration;
/* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- ) ;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( “%f seconds\n”, duration );
system(“pause”);
return 0;
}
这是一个求时间差的程序,那么为什么要除以CLK_TCK呢?
这是因为clock()是以毫秒为单位,要正确输出时间差需要把它换成秒,因此需要除以CLK_TCK。
clock()函数计算出来的是硬件滴答的数目,不是毫秒。在TC2.0中硬件每18.2个滴答是一秒,在VC++6.0中硬件每1000个滴答是一秒。