Mega Code Archive

 
Categories / Delphi / Forms
 

How to measure an elapsed time with a high resolution performance counter

Title: How to measure an elapsed time with a high-resolution performance counter implementation uses Windows, dialogs; procedure StartTimer; begin bOK := QueryPerformanceFrequency(Res); if bOK then QueryPerformanceCounter(t1); end; procedure StopTimer; begin if bOK then QueryPerformanceCounter(t2); end; procedure LoopRoutine; var iCounter: integer; begin StartTimer; // Put here some kind of loop: For, While or Repeat for i := 0 to iCounter do... { WHILE.... repeat...until} { After the Loop/ Nach Abschluss der Schleifenroutine:} if bOK then QueryPerformanceCounter(t2); end; procedure StartSchleife; begin LoopRoutine; if bOK then {$IFDEF VER80} // Show the elapsed time: MessageDlg('Der Schleifendurchlauf dauerte: ' + Format('%g Sekunden.', [(t2.QuadPart - t1.Quadpart) / Res.QuadPart])); $Else {$IFDEF VER120} MessageDlg('Der Schleifendurchlauf dauerte: ' + Format('%g Sekunden.', [(t2 - t1) / Res])); {$ENDIF} end;