QueryPerformanceFrequency - Procedure

Just starting out? Need help? Post your questions and find answers here.
jacky
User
User
Posts: 63
Joined: Mon Jan 21, 2019 1:41 pm

QueryPerformanceFrequency - Procedure

Post by jacky »

Hi,

I've got this from the forum somewhere:

Code: Select all

    Global QueryPerformanceFrequency.q : QueryPerformanceFrequency_(@QueryPerformanceFrequency)

    ; Returns the elapsed time between this call and the last call
    Procedure.d GetExecutionTime(unit.d=#Time_Millisecond)
      Protected counter.q, time.d
      Static QueryPerformanceCounter.q

      QueryPerformanceCounter_(@Counter)
      time = unit * (counter-QueryPerformanceCounter) / QueryPerformanceFrequency
      QueryPerformanceCounter = counter

      ProcedureReturn time
    EndProcedure
I can call it e.g. with:

Code: Select all

GetExecutionTime()
; Do something here...
Debug "Time to do something: " + GetExecutionTime() + " ms"
Is there a way to expand the procedure that I could measure overlapping times?

Like this:

Code: Select all

timeOuter.d = GetExecutionTime()
For i = 1 To 1000
  ; Do something
  timeInner.d = GetExecutionTime()
  For j = 1 To 1000
  ; Do something
  Next j
  Debug "Time for inner loop: " + GetExecutionTime(timeInner) + " ms"
Next i
Debug "Time for outer loop: " + GetExecutionTime(timeOuter) + " ms"
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: QueryPerformanceFrequency - Procedure

Post by Sicro »

Have a look at this code:
Poor (wo)man's time profiling
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
jacky
User
User
Posts: 63
Joined: Mon Jan 21, 2019 1:41 pm

Re: QueryPerformanceFrequency - Procedure

Post by jacky »

Thanks, but all of them aren't using QueryPerformanceCounter and I don't know if it's necessary to do it with 10 times the code of this little procedure...
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: QueryPerformanceFrequency - Procedure

Post by Demivec »

Just a note, PB's ElapsedMilliseconds() uses QueryPerformanceTimer if it is available so if you don't need dinner resolution than a millisecond it should work as well for you.
Post Reply