// High precision time
// The code is not compiled by myself
Unit ustopwatch;
Interface
Uses Windows, sysutils, dateutils;
Type
Tstopwatch = Class
Private
Ffrequency: tlargeinteger;
Fisrunning: Boolean;
Fishighresolution: Boolean;
Fstartcount, fstopcount: tlargeinteger;
Procedure settickstamp (VAR Lint: tlargeinteger );
Function getelapsedticks: tlargeinteger;
Function getelapsedmiliseconds: tlargeinteger;
Function getelapsed: string;
Public
Constructor create (const startoncreate: Boolean = false );
Procedure start;
Procedure stop;
Property ishighresolution: Boolean read fishighresolution;
Property elapsedticks: tlargeinteger read getelapsedticks;
Property elapsedmiliseconds: tlargeinteger read getelapsedmiliseconds;
Property elapsed: String read getelapsed;
Property isrunning: Boolean read fisrunning;
End;
Implementation
Constructor tstopwatch. Create (const startoncreate: Boolean = false );
Begin
Inherited create;
Fisrunning: = false;
Fishighresolution: = queryperformancefrequency (ffrequency );
If not fishighresolution then ffrequency: = msecspersec;
If startoncreate then start;
End;
Function tstopwatch. getelapsedticks: tlargeinteger;
Begin
Result: = fstopcount-fstartcount;
End;
Procedure tstopwatch. settickstamp (VAR Lint: tlargeinteger );
Begin
If fishighresolution then
Queryperformancecounter (lint)
Else
Lint: = millisecondof (now );
End;
Function tstopwatch. getelapsed: string;
VaR
DT: tdatetime;
Begin
DT: = elapsedmiliseconds/msecspersec/secsperday;
// Result: = format ('% d days, % s ',);
End;
Function tstopwatch. getelapsedmiliseconds: tlargeinteger;
Begin
Result: = (msecspersec * (fstopcount-fstartcount) Div ffrequency;
End;
Procedure tstopwatch. Start;
Begin
Settickstamp (fstartcount );
Fisrunning: = true;
End;
Procedure tstopwatch. Stop;
Begin
Settickstamp (fstopcount );
Fisrunning: = false;
End;
End.
(*
The following is an example of an application:
VaR
SW: tstopwatch;
Elapsedmiliseconds: Cardinal;
Begin
SW: = tstopwatch. Create ();
Try
Sw. Start;
// Timeoutthisfunction ()
Sw. Stop;
Elapsedmiliseconds: = Sw. elapsedmiliseconds;
Finally
Sw. Free;
End;
End;
Original Text connection
Http://delphi.about.com/od/windowsshellapi/a/delphi-high-performance-timer-tstopwatch.htm
Translation connection
Http://www.chinashare.net/index.asp? /4125-14-0-1-1.html
*)