COleDateTime
COleDateTime start = COleDateTime::GetCurrentTime();
COleDateTimeSpan span;
do {
MSG msg;
if (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
span = COleDateTime::GetCurrentTime() - start;
} while (span.GetTotalSeconds() <= 2);
可用 GetTotalMinutes、GetTotalHours、GetTotalDays 擴展至更長區間。
GetTickCount
DWORD dwStart = GetTickCount(), dwEnd = dwStart;
do {
MSG msg;
if (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
dwEnd = GetTickCount();
} while (dwEnd - dwStart <= 2000); // 2000 毫秒
QueryPerformanceCounter
LARGE_INTEGER li;
LONGLONG q1, q2;
double d = 0;
QueryPerformanceCounter(&li);
q1 = li.QuadPart;
while (d < 40) { // 40 微秒示例
QueryPerformanceCounter(&li);
q2 = li.QuadPart;
d = (double)(q2 - q1); // 需結合 QueryPerformanceFrequency 換算實際時間
}
注意:實際微秒換算需除以 QueryPerformanceFrequency 返回的頻率。
NOP 迴圈
#define NOP_COUNT 3
__asm {
MOV ECX, NOP_COUNT
DELAY:
NOP
LOOP DELAY
}
需根據 CPU 指令週期手動計算 NOP_COUNT,精度最高但可移植性最差,通常不建議在應用層使用。
COleDateTime。GetTickCount。QueryPerformanceCounter。