转自:(出处: ExcelHome技术论坛)
实现毫秒级和纳秒级计数的几个API--timeGetTime、GetTickCount、QueryPerformanceCoun
http://club.excelhome.NET/thread-907289-1-1.html
【你不是一个人在学习VB6】在此基础上做了少量改动,以更好地演示几个API的计时效果。
窗体上放一个命令按钮。窗体代码如下:
Option Explicit
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function QueryPerformanceCounter Lib "kernel32 " (lpPerformanceCount As Any) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32 " (lpFrequency As Any) As Long
Private Declare Sub Sleep Lib "KERNEL32.dll" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
timeCount
End Sub
Sub timeCount()
Dim curFrequency As Currency
Dim curStart As Currency
Dim curEnd As Currency
Dim delayms%
' delayms = 66
' delayms = 1
Randomize
delayms = Int((1000 - 1 + 1) * Rnd + 1)
Me.Caption = "要求 sleep " & delayms & "毫秒,实际结果是:"
QueryPerformanceFrequency curFrequency
QueryPerformanceCounter curStart '精度100ns
Sleep delayms
QueryPerformanceCounter curEnd
MsgBox "QueryPerformanceCounter计时: " & (curEnd - curStart) / curFrequency * 1000 & "毫秒"
curStart = Timer '精度55ms
Sleep delayms
MsgBox "Timer计时: " & (Timer - curStart) * 1000 & "毫秒"
curStart = GetTickCount '精度55ms
Sleep delayms
MsgBox "GetTickCount计时: " & (GetTickCount - curStart) & "毫秒"
curStart = timeGetTime '精度10ms
Sleep delayms
MsgBox "timeGetTime计时: " & (timeGetTime - curStart) & "毫秒"
Me.Caption = "请继续。。。"
End Sub