clock()和GetTickCount()

来源:互联网 发布:网络刷钱神豪小说 编辑:程序博客网 时间:2024/06/06 03:38

time.h中的声明:clock_t  clock(void);

这个函数返回从“程序启动”到“程序中调用clock()函数”之间的CPU时钟计时单元(clock tick)数。

time.h中还定义了一个符号常量——CLOCKS_PER_SEC,它代表每秒钟有多少个时钟计时单元(系统时间单位数)。

例如:

clock_t start = clock();

……

clock_t end = clock();

float duration = float(end - start)/CLOCKS_PER_SEC;

duration为……持续的秒数。


GetTickCount()返回从操作系统启动到当前所经过的毫秒数。使用前包含windows.h。

//CPU忙闲比1:1int busyTime = 10;int idleTime = busyTime;INT64 startTime = 0;    while (true){startTime = GetTickCount();while (GetTickCount() - startTime <= busyTime);Sleep(idleTime);}


0 0