CLK_TCK

王朝百科·作者佚名  2011-03-18  
宽屏版  字体: |||超大  

TC2.0中头文件time.h下宏定义的符号常量。在VC++6.0中也有关于CLK_TCK的宏定义。不过在两个版本中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。其值为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个滴答是一秒。

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝百科 版权所有