Elapsed Time - GetTickCount

Category: Time/Timers

Date: 02-16-2022

Return to Index


 
'Applications often need to compute elapsed time between two events, for reasons
'such as comparing the time it takes two different algorithms to execute.  The
'four basic approaches are:
 
'TIMER (PowerBASIC) - elapsed time since midnight
'TIX (PowerBASIC) - CPU cycles since last use of TIX
'GetTickCount() API
'QueryPerformanceCount() API
 
'For absolute accuracy in comparing time to execute events, TIX and QueryPerformanceCounter
'are the best choices. With QueryPerformanceCounter you can convert to time, so it is more 
'flexible.  Timer and GetTickCount are easier to use, but less accurate. For simple convenience
'and reasonable accuracy on longer intervals, programmers often rely on GetTickCount.
 
 
'Primary Code:
   Local iStart,iEnd As Long
   iStart = GetTickCount
   '---------------------------------------
   '... do something
   '---------------------------------------
   iEnd = GetTickCount
   ? Format$((iEnd - iStart)/1000,3) & " seconds"
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
Global qFreq, qStart, qStop AS QUAD
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, 100,"Test", 20,10,120,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Select Case Cb.Msg
      Case %WM_Command
         Select Case Cb.Ctl
            Case 100 : SpeedTest
         End Select
   End Select
End Function
 
Sub SpeedTest
   Local iStart,iEnd As Long
   iStart = GetTickCount
   '---------------------------------------
   '... do something
   '---------------------------------------
   iEnd = GetTickCount
   ? Format$((iEnd - iStart)/1000,3) & " seconds"
End Sub
 
'gbs_00261
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm