Three delay methods in Delphi and their timing precision analysis

Source: Internet
Author: User
Tags sleep function
Three delay methods in Delphi and their timing precision analysis

From: lili_0522 time: 2005-7-14 16:21:05
In Delphi, you can use the following three methods to implement program latency: tttimer control, sleep function, and gettickcount function. However, the precision varies.
Introduction to methods 1 and 3

1) tttimer Control

The tttimer control is implemented by calling the timer functions settimer and killtimer of Windows API, and simplifies the process of processing wm_timer messages. By setting the ontimer event and interval attribute, we can easily generate some simple scheduled events.

2) sleep function

The sleep function is used to set the execution latency of a program. Sleep is called in the form of sleep (milliseconds). The current process is paused in milliseconds. Sleep is actually a sleep function that calls windows APIs. For example:

Sleep (1000); // latency: 1000 ms

Sleep will cause program stagnation. If you delay for a long time, your program will not be able to respond to other messages that occur during the delay period, so the program seems to be temporarily suspended.

3) gettickcount Function

Latency in the main program. In order to achieve the two goals of latency and response message, the cycle of gettickcount () is a widely spread method. For example:

Procedure delay (msecs: longint );
// Latency function. The unit of msecs is Millisecond (1 second in kilobytes)
VaR
Firsttickcount, now: longint;
Begin
Firsttickcount: = gettickcount ();
Repeat
Application. processmessages;
Now: = gettickcount ();
Until (now-firsttickcount> = msecs) or (now <firsttickcount );
End;

II. Introduction to high-resolution Performance Counter

To compare the accuracy of the above method, you must first find a reference timer. Here, I provide two reference timers. One is to use a single-chip microcomputer every 1.024ms to generate a Real-Time Interrupt RTI, as a counter; the second is to choose a high-precision delicate level performance counter (see: http://msdn.microsoft.com/msdnmag/issues/04/03/HighResolutionTimer/default.aspx, or http://community.csdn.net/Expert/FAQ/FAQ_Index.asp? Id = 200249
)

1) Delphi source code of the counter

{
A high-precision counter/Timer. retrieves time differences
Downto microsec.
Quick Reference:
Thpcounter inherits from tcomponent.

Key-methods:
Start: starts the counter. Place this call just before
Code you want to measure.

Read: reads the counter as a string. Place this call just
After the code you want to measure.

Readint: reads the counter as an int64. place this call just
After the code you want to measure.
--------------------------------------------------------------------------------
}
Unit hpcounter;

Interface

Uses
Sysutils, wintypes, winprocs, messages, classes, graphics, controls,
Forms, dialogs, stdctrls, extctrls;

Type
Tint64 = tlargeinteger;
Thpcounter = Class (tcomponent)
Private
Frequency: tlargeinteger;
Lpperformancecount1: tlargeinteger;
Lpperformancecount2: tlargeinteger;
Fabout: string;
Procedure setabout (value: string );
{Private Declarations}
Public
Constructor create (aowner: tcomponent); override;
Destructor destroy; override;
Procedure start;
Function read: string;
Function readint: tlargeinteger;
{Private Declarations}
Published
Property about: String read fabout write setabout;
{Published Declarations}
End;

Procedure register;

Implementation

Procedure register;
Begin
Registercomponents ('mas prod. ', [thpcounter]);
End;

Constructor thpcounter. Create (aowner: tcomponent );
Begin
Inherited create (aowner );
Fabout: = 'version 1.1, 2000 & reg; mats Asplund, email: masprod@telia.com, site: http://go.to/masdp ';
End;

Destructor thpcounter. Destroy;
Begin
Inherited destroy;
End;

Function thpcounter. Read: string;
Begin
Queryperformancecounter (tint64 (@ lpperformancecount2) ^ ));
Queryperformancefrequency (tint64 (@ frequency) ^ ));
Result: = inttostr (round (1000000 * (lpperformancecount2-
Lpperformancecount1)/frequency ));
End;

Function thpcounter. readint: tlargeinteger;
Begin
Queryperformancecounter (tint64 (@ lpperformancecount2) ^ ));
Queryperformancefrequency (tint64 (@ frequency) ^ ));
Result: = round (1000000 * (lpperformancecount2-
Lpperformancecount1)/frequency );
End;

Procedure thpcounter. setabout (value: string );
Begin
Exit;
End;

Procedure thpcounter. Start;
Begin
Queryperformancecounter (tint64 (@ lpperformancecount1) ^ ));
End;

End.

2) usage:
Unit unit1;

Interface

Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
Hpcounter, stdctrls;

Type
Tform1 = Class (tform)
Button1: tbutton;
Edit1: tedit;
Label1: tlabel;
Label2: tlabel;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;

VaR
Form1: tform1;

Implementation

{$ R *. DFM}

Procedure tform1.button1click (Sender: tobject );
Begin
Edit1.text: = '';
Application. processmessages;
With thpcounter. Create (Self) Do
Begin
Start;
// Place code to measure here
Sleep (1000 );
// Place code to measure here
Edit1.text: = read;
Free;
End;
End;

End.

2. Comparison of precision between the three methods

For comparison, the above three methods are used, set the delay time to 1 ms, 2 ms, 5 ms, 10 ms, 20 ms, 50 ms, 100 ms, 200 ms, 500 ms, and 1000 ms, respectively, the number of cycles is 5, and the actual delay time is obtained.

1) tttimer Control

Actual latency (MS)
1 ms: 8.012 21.551 6.875 21.647
2 MS: 9.957 20.675 14.671 11.903
5 ms: 9.952 20.605 9.924 20.705
10 MS: 14.852 9.96 21.547 9.82
20 ms: 27.512 34.291 26.427 31.244
50 ms: 61.196 61.307 64.027 62.048
100 MS: 102.495 108.408 112.318 110.322
200 ms: 193.955 202.135 207.016 205.082
500 ms: 496.659 500.534 503.398 495.551
1000 ms: 999.699 1003.576 993.698 1004.443

2) sleep function

1 ms: 1.895 1.895 1.896 1.897
2 MS: 2.868 2.874 2.852 2.872
5 ms: 5.8 5.797 5.79 5.79
10 MS: 10.675 10.683 10.611 10.669
20 ms: 20.404 20.434 20.447 20.477
50 ms: 50.67 50.691 50.69 50.682
100 MS: 100.515 100.469 100.484 100.481
200 ms: 200.101 200.126 199.892 200.066
500 ms: 499.961 499.961 499.958 499.961
1000 ms: 1000.034 1000.04 1000.03 1000.018

3) gettickcount Function

1 ms: 15.54 15.596 15.527 15.566
2 MS: 15.561 15.563 15.603 15.477
5 ms: 15.519 15.549 15.569 15.666
10 MS: 15.558 15.561 15.522 15.568
20 ms: 31.186 31.137 31.17 31.17
50 ms: 62.445 62.4 63.893 60.88
100 MS: 109.276 109.298 109.273 109.28
200 ms: 203.027 203.084 203.021 203.027
500 ms: 499.959 499.961 499.963 499.967
1000 ms: 1000.023 1000.022 1000.026 1000.029

It can be seen that the sleep function has the highest precision, especially the latency within 10 ms. Ttimer controls have the worst timing accuracy, poor stability, and high volatility. The shortest latency of the gettickcount function is about 15 ms, and the stability is better than that of ttimer.

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.