High-precision time, interval class (excerpt)

Source: Internet
Author: User

// 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

 

*)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.