Date: 02-16-2022
Return to Index
created by gbSnippets
http://support.microsoft.com/kb/274323
http://support.microsoft.com/kb/895980
'QueryPerformance
Global qFreq, qStart, qStop As QUAD, DeltaTime As Single
QueryPerformanceFrequency qFreq
QueryPerformanceCounter qStart
'... do something
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? "Results: " + str$(qStop-qStart) + " " + Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
'GetTickCount
Global iStart, iEnd As Long
iStart = GetTickCount
'... do something
iEnd = GetTickCount
? "Results (Elapsed Time): " + Format$((iStop - iStart)/1000,3) & " seconds"
'TIX
Global qVar
Tix qVar
'... do something
Tix qVEnd
'Verify Support of QueryPerformanceCounter
#Compile EXE
#Include "Win32API.inc"
Function PBMain() As Long
Local qFreq As Quad
QueryPerformanceFrequency qFreq
If qFreq = 0 Then
? "PC does not support QueryPerformanceCounter"
Else
? Format$(qFreq,"###,###,###,###")
End If
End Function
'Get HRPC Frequency
#Compile EXE
#Include "Win32API.inc"
Function PBMain() As Long
Local HRPCFreq As Quad
QueryPerformanceFrequency HRPCFreq
? Format$(HRPCfreq,"###,###,###,###")
End Function
'Rough estimate of CPU Frequency (SLEEP is inaccurate)
#Compile EXE
Function PBMain() As Long
Local CPUFreq As Quad
Tix CPUFreq
Sleep 1000
Tix End CPUFreq
? Format$(CPUfreq,"###,###,###,###")
End Function
'Better estimate of CPU Frequency
#Compile EXE
#Include "win32api.inc"
Function PBMain() As Long
Local qFreq,qStart,qStop,qValue As Quad, qTime As Single
QueryPerformanceFrequency qFreq
QueryPerformanceCounter qStart
Tix qValue
Sleep 1000
Tix End qValue
QueryPerformanceCounter qStop
qTime = (qStop-qStart)/qFreq
? Format$(qValue/qTime,"###,###,###,###")
End Function
'CPU Frequency From Registry
#Compile EXE
#Dim All
%Unicode=1
#Include "WIN32API.INC"
Function PBMain
Local hKey, CPUSpeed, KeyValue As Long, RegKey As Asciiz * %Max_Path
RegKey = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
KeyValue = &H80000002
RegOpenKey(KeyValue, RegKey, hKey)
RegQueryValueEx(hKey, "~MHz", 0, 0, CPUSpeed, 4)
RegCloseKey(hKey)
? Format$(CPUSpeed,"##,###") + " MHz"
End Function
'Dave Roberts QueryPerformance Macros
Macro InitializeTimer
Local qFreq, qOverhead, qStart, qStop As Quad
Local f As String
f = "#####.##"
QueryPerformanceFrequency qFreq
QueryPerformanceCounter qStart ' Intel suggestion. First use may be suspect
QueryPerformanceCounter qStart ' So, wack it twice <smile>
QueryPerformanceCounter qStop
qOverhead = qStop - qStart ' Relatively small
End Macro
Macro StartTimer = QueryPerformanceCounter qStart
Macro StopTimer = QueryPerformanceCounter qStop
Macro TimeTaken = Using$(f,(qStop - qStart - qOverhead)*1000/qFreq) + "ms"
'Measure System Timer Frequency
#Compile EXE
#Dim All
%Unicode=1
#Include "WIN32API.INC"
Function PBMain
Local cnt, Count, Last, Curr ,qFreq, StartTime, EndTime As Quad
'Don't start until System Timer has just been updated
Last = GetTickCount
Do : Loop Until GetTickCount <> Last
Last = GetTickCount
'Count how many times System Timer changes value over an arbitrary period of time (loops)
QueryPerformanceFrequency qFreq
QueryPerformanceCounter StartTime
cnt = 100 'arbitrary number of loops. almost anything over 5-10 works.
Do
Curr = GetTickCount
If Curr <> Last Then : Incr Count : Last = Curr
Loop Until Count = cnt
QueryPerformanceCounter EndTime
? "System Timer runs at about" + Str$(cnt*qFreq/(EndTime-StartTime)) + "Hz."
End Function
'How Accurate is SLEEP?
#Compile Exe
#Include "WIN32API.INC"
Function PBMain() As Long
Local qFreq, qStart, qStop As Quad
Local iStart, iEnd As Long
Local DeltaTime As Single
QueryPerformanceFrequency qFreq
QueryPerformanceCounter qStart
iStart = GetTickCount
Sleep 1000
iEnd = GetTickCount
QueryPerformanceCounter qStop
DeltaTime = (qStop-qStart)/qFreq
? Str$(DeltaTime)
End Function
'Programmable Timer Example
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%ID_Timer = 500
%ID_Button = 501
Global hDlg As Dword, Flag, timerInterval As Long
Function PBMain () As Long
Dialog New Pixels, 0, "Timer",,, 150,100, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %ID_Button, "Start/Stop Timer", 10,10,120,20
Dialog Show Modal hDlg, Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_InitDialog
Flag = 1 'timer is one
timerInterval = 500 'timer interval in milliseconds
SetTimer(Cb.Hndl, %ID_Timer, timerInterval, ByVal %NULL) 'create timer
Dialog Post Cb.Hndl, %WM_Timer, %ID_TIMER, 0 'force initial %WM_TIMER (optional)
Case %WM_Command
Select Case Cb.Ctl
Case %ID_Button
Flag = Flag Xor 1
If Flag Then
SetTimer(Cb.Hndl, %ID_Timer, timerInterval, ByVal %NULL) 'create timer
Dialog Post Cb.Hndl, %WM_Timer, %ID_TIMER, 0 'force initial %WM_TIMER (optional)
Else
KillTimer Cb.Hndl, %ID_Timer
End If
End Select
Case %WM_Timer
Dialog Set Text hDlg, Time$ 'do something in each intervale
Case %WM_Destroy
KillTimer Cb.Hndl, %ID_Timer
End Select
End Function
'gbs_00758
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm