如题所述
å¨ç²¾åº¦è¦æ±è¾é«çæ
åµä¸ï¼å¦è¦æ±è¯¯å·®ä¸å¤§äº1msæ¶ï¼å¯ä»¥å©ç¨GetTickCount()å½æ°ã该å½æ°çè¿åå¼æ¯DWORDåï¼è¡¨ç¤ºä»¥ms为åä½ç计ç®æºå¯å¨åç»åçæ¶é´é´éãä¸åç代ç å¯ä»¥å®ç°50msç精确å®æ¶ï¼å
¶è¯¯å·®å°äº1msã
// èµ·å§å¼åä¸æ¢å¼
DWORD dwStart, dwStop ;
dwStop = GetTickCount();
while(TRUE) {
// ä¸ä¸æ¬¡çä¸æ¢å¼åææ°çèµ·å§å¼
dwStart = dwStop ;
// æ¤å¤æ·»å ç¸åºæ§å¶è¯å¥
do
{
dwStop = GetTickCount() ;
}while(dwStop ï¼ 50 < dwStart) ;
}
å¾®è½¯å ¬å¸å¨å ¶å¤åªä½Windowsä¸æä¾äºç²¾ç¡®å®æ¶å¨çåºå±APIæ¯æãå©ç¨å¤åªä½å®æ¶å¨å¯ä»¥å¾ç²¾ç¡®å°è¯»åºç³»ç»çå½åæ¶é´ï¼å¹¶ä¸è½å¨é常精确çæ¶é´é´éå å®æä¸ä¸ªäºä»¶ãå½æ°æè¿ç¨çè°ç¨ãå©ç¨å¤åªä½å®æ¶å¨çåºæ¬åè½ï¼å¯ä»¥éè¿ä¸¤ç§æ¹æ³å®ç°ç²¾ç¡®å®æ¶ã
1.使ç¨timeGetTime()å½æ°
该å½æ°å®æ¶ç²¾åº¦ä¸ºms级ï¼è¿åä»Windowså¯å¨å¼å§æç»è¿çæ¶é´ãç±äºä½¿ç¨è¯¥å½æ°æ¯éè¿æ¥è¯¢çæ¹å¼è¿è¡å®æ¶æ§å¶çï¼æ以ï¼åºè¯¥å»ºç«å®æ¶å¾ªç¯æ¥è¿è¡å®æ¶äºä»¶çæ§å¶ã
2. 使ç¨timeSetEvent()å½æ°
å©ç¨è¯¥å½æ°å¯ä»¥å®ç°å¨ææ§çå½æ°è°ç¨ãå½æ°çåæ°è¯´æå¦ä¸ï¼
uDelayï¼å»¶è¿æ¶é´ï¼
uResolutionï¼æ¶é´ç²¾åº¦ï¼å¨Windowsä¸ç¼ºçå¼ä¸º1msï¼
lpFunctionï¼åè°å½æ°ï¼ä¸ºç¨æ·èªå®ä¹å½æ°ï¼å®æ¶è°ç¨ï¼
dwUserï¼ç¨æ·åæ°ï¼
uFlagsï¼æ å¿åæ°ï¼
TIME_ONESHOTï¼æ§è¡ä¸æ¬¡ï¼
TIME_PERIODICï¼å¨ææ§æ§è¡ã
å ·ä½åºç¨æ¶ï¼å¯ä»¥éè¿è°ç¨timeSetEvent()å½æ°ï¼å°éè¦å¨ææ§æ§è¡çä»»å¡å®ä¹å¨lpFunctionåè°å½æ°ä¸(å¦ï¼å®æ¶éæ ·ãæ§å¶ç)ï¼ä»èå®ææéå¤ççäºä»¶ãéè¦æ³¨æçæ¯ï¼ä»»å¡å¤ççæ¶é´ä¸è½å¤§äºå¨æé´éæ¶é´ãå¦å¤ï¼å¨å®æ¶å¨ä½¿ç¨å®æ¯åï¼åºåæ¶è°ç¨timeKillEvent()å°ä¹éæ¾
对äºç²¾ç¡®åº¦è¦æ±æ´é«çå®æ¶æä½ï¼ååºè¯¥ä½¿ç¨QueryPerformanceFrequency()åQueryPerformanceCounter()å½æ°ãè¿ä¸¤ä¸ªå½æ°æ¯ç³»ç»æä¾ç精确æ¶é´å½æ°ï¼å¹¶è¦æ±è®¡ç®æºä»ç¡¬ä»¶ä¸æ¯æ精确å®æ¶å¨ãQueryPerformanceFrequency()å½æ°åQueryPerformanceCounter()å½æ°çååå¦ä¸ï¼
BOOL QueryPerformanceFrequency(LARGE_INTEGER ï¼lpFrequency);
BOOL QueryPerformanceCounter(LARGE_INTEGER ï¼lpCount);
æ°æ®ç±»åLARGE_INTEGERæ¢å¯ä»¥æ¯ä¸ä¸ª8åèé¿çæ´åæ°ï¼ä¹å¯ä»¥æ¯ä¸¤ä¸ª4åèé¿çæ´åæ°çèåç»æï¼å ¶å ·ä½ç¨æ³æ ¹æ®ç¼è¯å¨æ¯å¦æ¯æ64ä½èå®ã
å¨è¿è¡å®æ¶ä¹åï¼å è°ç¨QueryPerformanceFrequency()å½æ°è·å¾æºå¨å é¨å®æ¶å¨çæ¶éé¢çï¼ç¶åå¨éè¦ä¸¥æ ¼å®æ¶çäºä»¶åçä¹åååçä¹ååå«è°ç¨QueryPerformanceCounter()å½æ°ï¼å©ç¨ä¸¤æ¬¡è·å¾ç计æ°ä¹å·®åæ¶éé¢çï¼è®¡ç®åºäºä»¶ç»åç精确æ¶é´ã
// èµ·å§å¼åä¸æ¢å¼
DWORD dwStart, dwStop ;
dwStop = GetTickCount();
while(TRUE) {
// ä¸ä¸æ¬¡çä¸æ¢å¼åææ°çèµ·å§å¼
dwStart = dwStop ;
// æ¤å¤æ·»å ç¸åºæ§å¶è¯å¥
do
{
dwStop = GetTickCount() ;
}while(dwStop ï¼ 50 < dwStart) ;
}
å¾®è½¯å ¬å¸å¨å ¶å¤åªä½Windowsä¸æä¾äºç²¾ç¡®å®æ¶å¨çåºå±APIæ¯æãå©ç¨å¤åªä½å®æ¶å¨å¯ä»¥å¾ç²¾ç¡®å°è¯»åºç³»ç»çå½åæ¶é´ï¼å¹¶ä¸è½å¨é常精确çæ¶é´é´éå å®æä¸ä¸ªäºä»¶ãå½æ°æè¿ç¨çè°ç¨ãå©ç¨å¤åªä½å®æ¶å¨çåºæ¬åè½ï¼å¯ä»¥éè¿ä¸¤ç§æ¹æ³å®ç°ç²¾ç¡®å®æ¶ã
1.使ç¨timeGetTime()å½æ°
该å½æ°å®æ¶ç²¾åº¦ä¸ºms级ï¼è¿åä»Windowså¯å¨å¼å§æç»è¿çæ¶é´ãç±äºä½¿ç¨è¯¥å½æ°æ¯éè¿æ¥è¯¢çæ¹å¼è¿è¡å®æ¶æ§å¶çï¼æ以ï¼åºè¯¥å»ºç«å®æ¶å¾ªç¯æ¥è¿è¡å®æ¶äºä»¶çæ§å¶ã
2. 使ç¨timeSetEvent()å½æ°
å©ç¨è¯¥å½æ°å¯ä»¥å®ç°å¨ææ§çå½æ°è°ç¨ãå½æ°çåæ°è¯´æå¦ä¸ï¼
uDelayï¼å»¶è¿æ¶é´ï¼
uResolutionï¼æ¶é´ç²¾åº¦ï¼å¨Windowsä¸ç¼ºçå¼ä¸º1msï¼
lpFunctionï¼åè°å½æ°ï¼ä¸ºç¨æ·èªå®ä¹å½æ°ï¼å®æ¶è°ç¨ï¼
dwUserï¼ç¨æ·åæ°ï¼
uFlagsï¼æ å¿åæ°ï¼
TIME_ONESHOTï¼æ§è¡ä¸æ¬¡ï¼
TIME_PERIODICï¼å¨ææ§æ§è¡ã
å ·ä½åºç¨æ¶ï¼å¯ä»¥éè¿è°ç¨timeSetEvent()å½æ°ï¼å°éè¦å¨ææ§æ§è¡çä»»å¡å®ä¹å¨lpFunctionåè°å½æ°ä¸(å¦ï¼å®æ¶éæ ·ãæ§å¶ç)ï¼ä»èå®ææéå¤ççäºä»¶ãéè¦æ³¨æçæ¯ï¼ä»»å¡å¤ççæ¶é´ä¸è½å¤§äºå¨æé´éæ¶é´ãå¦å¤ï¼å¨å®æ¶å¨ä½¿ç¨å®æ¯åï¼åºåæ¶è°ç¨timeKillEvent()å°ä¹éæ¾
对äºç²¾ç¡®åº¦è¦æ±æ´é«çå®æ¶æä½ï¼ååºè¯¥ä½¿ç¨QueryPerformanceFrequency()åQueryPerformanceCounter()å½æ°ãè¿ä¸¤ä¸ªå½æ°æ¯ç³»ç»æä¾ç精确æ¶é´å½æ°ï¼å¹¶è¦æ±è®¡ç®æºä»ç¡¬ä»¶ä¸æ¯æ精确å®æ¶å¨ãQueryPerformanceFrequency()å½æ°åQueryPerformanceCounter()å½æ°çååå¦ä¸ï¼
BOOL QueryPerformanceFrequency(LARGE_INTEGER ï¼lpFrequency);
BOOL QueryPerformanceCounter(LARGE_INTEGER ï¼lpCount);
æ°æ®ç±»åLARGE_INTEGERæ¢å¯ä»¥æ¯ä¸ä¸ª8åèé¿çæ´åæ°ï¼ä¹å¯ä»¥æ¯ä¸¤ä¸ª4åèé¿çæ´åæ°çèåç»æï¼å ¶å ·ä½ç¨æ³æ ¹æ®ç¼è¯å¨æ¯å¦æ¯æ64ä½èå®ã
å¨è¿è¡å®æ¶ä¹åï¼å è°ç¨QueryPerformanceFrequency()å½æ°è·å¾æºå¨å é¨å®æ¶å¨çæ¶éé¢çï¼ç¶åå¨éè¦ä¸¥æ ¼å®æ¶çäºä»¶åçä¹åååçä¹ååå«è°ç¨QueryPerformanceCounter()å½æ°ï¼å©ç¨ä¸¤æ¬¡è·å¾ç计æ°ä¹å·®åæ¶éé¢çï¼è®¡ç®åºäºä»¶ç»åç精确æ¶é´ã
温馨提示:答案为网友推荐,仅供参考