Transferred from Baidu Library:
Delayus
1 void DelayUs(int uDelay) 2 { 3 4 LARGE_INTEGER litmp; 5 LONGLONG QPart1,QPart2; 6 7 double dfMinus,dfFreq,dfTim; 8 9 /*10 Pointer to a variable that the function sets, in counts per second, to the current performance-counter frequency. 11 If the installed hardware does not support a high-resolution performance counter, 12 the value passed back through this pointer can be zero. 13 14 */15 QueryPerformanceFrequency(&litmp);16 17 dfFreq = (double)litmp.QuadPart;18 19 /*20 Pointer to a variable that the function sets, in counts, to the current performance-counter value. 21 */22 QueryPerformanceCounter(&litmp);23 24 QPart1 = litmp.QuadPart;25 do26 {27 QueryPerformanceCounter(&litmp);28 QPart2 = litmp.QuadPart;29 dfMinus = (double)(QPart2-QPart1);30 dfTim = dfMinus/dfFreq;31 }while(dfTim<0.000001 * uDelay);32 }
If you understand the above functions, you can also use some of the Code in it to test the execution time of many functions.
Of course, this function is still a bit risky. If the timer overflows during a delay period, this value may be inaccurate and needs to be handled with caution.
For example, if (dfminus <0)
Break;
This assumes that the hardware timer is counted in an increase.