在講應用前再補充一些
'GetTickCount()
'Windows NT 3.5 及更高版本,精度為 10ms
'Windows NT 3.1 及更高版本,精度為 16ms
'Windows 95 及更高版本,精度為 55ms
'timeGetTime()
'精度約1ms , 此式適用於大多數應用場合
Public Declare Function timeGetTime Lib "winmm.dll" () As Long
'1s = 1000ms = 1000000μs = 1000000000ns
那微秒級數的呢?
'得到處理器使用的主機板內部計時器的時鐘頻率
Private Declare Function QueryPerformanceFrequency Lib "kernel32" ( _
lpFrequency As Currency) As Long 'LARGE_INTEGER
Private Declare Function QueryPerformanceCounter Lib "kernel32" ( _
lpPerformanceCount As Currency) As Long 'LARGE_INTEGER
原本QueryPerformanceFrequency 和 QueryPerformanceCounter 的參數之資料型態是 LARGE_INTEGER
Type LARGE_INTEGER '佔 8 byte
lowpart As Long
highpart As Long
End Type
因為要簡省換算是改成同樣是 8byte 的 Currency(for VB,C 的話可用 LONGLONG)
=================
timeGetTime 的實例同第一例子
所以這裡示範 高精度(微秒)
if not GetTickCount_MicroSec(True) then
MsgBox "不支持高精度計數器!"
else
Do
Call S()
DoEvents
ReviseTimerTickMicroSec = GetTickCount_MicroSec
Loop Until ReviseTimerTickMicroSec > 1000 '1000us=1ms
endif
.....
Public Function GetTickCount_MicroSec(Optional ByRef blnCountStart As Boolean)
Static TickCount1 As Currency
Dim TickCount2 As Currency
Dim Freq As Currency
If QueryPerformanceFrequency(Freq) Then
If blnCountStart Then
QueryPerformanceCounter TickCount1
GetTickCount_MicroSec = True
Else
QueryPerformanceCounter TickCount2
GetTickCount_MicroSec = (Abs(TickCount2 - TickCount1) / Freq) * 1000000 '10^6
End If
Else
'MsgBox "不支持高精度計數器!"
GetTickCount_MicroSec = False
End If
End Function
|