???? ID: 172338 - ????? ???????: 02 ?????? 2010 - ??????: 4.0

??? ??? ??? QueryPerformanceCounter ?? ????? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

???????? bottlenecks ?? ??????? ?? ??? ??? timing, ?? ?? ??? ???? ?? ??? ?????? ??? ???? ??????????? ????? ?? ????? ???? ????? ??? ?? ???? ????? ???? ?? ?? ??? ????????? ??? ?? ??? QueryPerformanceCounter ?????? ?? ????? ???? ?????

???? ???????

Differing ??????? ?? ?? ??????? ???????? ?????? ?????? ??? ???? ?? ??? ??:
Function                 Units                      Resolution
---------------------------------------------------------------------------
Now, Time, Timer         seconds                    1 second
GetTickCount             milliseconds               approx. 10 ms
TimeGetTime              milliseconds               approx. 10 ms
QueryPerformanceCounter  QueryPerformanceFrequency  same
				
??? ???? ?????? ??? high-resolution ?????? ?? ?????? ???? ??, ?? ?? ????? ?? ???? QueryPerformanceCounter ?? QueryPerformanceFrequency high-resolution ??????? ???? ?? ????

????? ????? ??? ?? ??????? ?????? ????? ???? ??:

???????: ???? ?? ????? ?? ?? ?? ???? ??? ??? ?? ??? ?? ???? ????? ??? Microsoft ???? ?? ??????, ?? ?? ?????? ?? ???????, ?????? ?? ?????? ??/?? ???? ????? ???????? ?? ??? ????????? ???? ?? ??? ????? ???? ????? ???? ?? ?????? ??? ???? ?? ??? ?? "???? ??" ?????? ????? ???

Step-by-Step ????????????

  1. ???? ??????? ??? ????? ??? ???? ????? ??? ?? ??? ???? ???? ??? ?? ????, ??????? ?? ??????? ???????, ????? declarations ?????
           Option Explicit
    
          Declare Function QueryPerformanceCounter Lib "Kernel32" _
                                     (X As Currency) As Boolean
          Declare Function QueryPerformanceFrequency Lib "Kernel32" _
                                     (X As Currency) As Boolean
          Declare Function GetTickCount Lib "Kernel32" () As Long
          Declare Function timeGetTime Lib "winmm.dll" () As Long
    
          Sub Test_Timers()
          Dim Ctr1 As Currency, Ctr2 As Currency, Freq As Currency
          Dim Count1 As Long, Count2 As Long, Loops As Long
          '
          ' Time QueryPerformanceCounter
          '
            If QueryPerformanceCounter(Ctr1) Then
              QueryPerformanceCounter Ctr2
              Debug.Print "Start Value: "; Format$(Ctr1, "0.0000")
              Debug.Print "End Value: "; Format$(Ctr2, "0.0000")
              QueryPerformanceFrequency Freq
              Debug.Print "QueryPerformanceCounter minimum resolution: 1/" & _
                          Freq * 10000; " sec"
              Debug.Print "API Overhead: "; (Ctr2 - Ctr1) / Freq; "seconds"
            Else
              Debug.Print "High-resolution counter not supported."
            End If
          '
          ' Time GetTickCount
          '
            Debug.Print
            Loops = 0
            Count1 = GetTickCount()
            Do
              Count2 = GetTickCount()
              Loops = Loops + 1
            Loop Until Count1 <> Count2
            Debug.Print "GetTickCount minimum resolution: "; _
                        (Count2 - Count1); "ms"
            Debug.Print "Took"; Loops; "loops"
          '
          ' Time timeGetTime
          '
            Debug.Print
            Loops = 0
            Count1 = timeGetTime()
            Do
              Count2 = timeGetTime()
              Loops = Loops + 1
            Loop Until Count1 <> Count2
            Debug.Print "timeGetTime minimum resolution: "; _
                        (Count2 - Count1); "ms"
            Debug.Print "Took"; Loops; "loops"
          End Sub
    						
  2. ????/??????? ????? ?? ??????? ?? ?????? ???? ?????? ????? ?? ???? ????? ???? ?????:
    ??? ???????: 3516284.3498
    ??? ?? ??????: 3516284.3521
    ??????? ?????????? QueryPerformanceCounter: 1/1193182 ?????
    API Overhead: 1.92761875388667E - 05 ?????

    ??????? ?????????? GetTickCount: 10 ms
    650 Loops ???? ???

    ??????? ?????????? timeGetTime: 10 ms
    1565 Loops ???? ???
?????? ??? ????????? ???? ?? ???? GetTickCount ?? timeGetTime ??? ???????? ??????? ??? Loops ?? ???????? ?????? ????????? ????? ???????? ?????? ?? ????????? ?? ??? ?? ?? ???? ?? ???-??? ?????

?? ????? ???, QueryPerformanceCounter successive API ???, high-resolution ??? ?? ?????? ??? ???? usefulness ????? ?? ??? ??? ????????? ???? ??? ?????? ?? ?????? ??? ?? ?? microsecond ?? ???? ??? ?????????? ?? ??????-?????? ??, ??????? ?? measures ??? ???? ??????? ???? ????? ?? ?????? ??? ?? ????????? ???? ?? ??? QueryPerformanceFrequency ?? ???? ?? ??????? ???? ?? ??? ??? ?????? ??? ???, ???? API ??? ?? ??? overhead ?? ???? 19 microseconds. ?? ?? ???? ??? ?? ??????????? timing subtracted ???? ?? ???? ?? ????:
   Private Sub Time_Addition()
   Dim Ctr1 As Currency, Ctr2 As Currency, Freq As Currency
   Dim Overhead As Currency, A As Long, I As Long
     QueryPerformanceFrequency Freq
     QueryPerformanceCounter Ctr1
     QueryPerformanceCounter Ctr2
     Overhead = Ctr2 - Ctr1        ' determine API overhead
     QueryPerformanceCounter Ctr1  ' time loop
     For I = 1 To 100
       A = A + I
     Next I
     QueryPerformanceCounter Ctr2
     Debug.Print "("; Ctr1; "-"; Ctr2; "-"; Overhead; ") /"; Freq
     Debug.Print "100 additions took";
     Debug.Print (Ctr2 - Ctr1 - Overhead) / Freq; "seconds"
   End Sub
				
Sample ??????:
(3630876.6256-3630876.6388-0.0013) / 119.3182
100 ??????? ?? 9.97333181358753E ???? ??? ?? - 05 ?????
????? ???: ?????? ?? ????? ???? ???? ??, ??????? ??? ?? ??? ???????? ?????? ?? ???? 10000 ???? ????? ?? ???? ?? ?? ??? ???????? involves, ??????? ?? ??????? ???? ???

??????

Microsoft ?????? ??????? ??; ????: timeGetTime GetTickCount QueryPerformanceCounter QueryPerformanceFrequency

???? ???? ???? ??:
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Access 2002 Standard Edition
  • Microsoft Access 2000 Standard Edition
  • Microsoft Access 97 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 Standard Edition
??????: 
kbhowto kbprogramming kbmt KB172338 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:172338  (http://support.microsoft.com/kb/172338/en-us/ )