clock()與GetTickCount()的區別與作用
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()返回從
- //CPU忙閒比1:1
- int busyTime = 10;
- int idleTime = busyTime;
- INT64 startTime = 0;
- while (true){
- startTime = GetTickCount();
- while (GetTickCount() - startTime <= busyTime) ;
- Sleep(idleTime);
- }