Delphi High Precision Timing method

Source: Internet
Author: User
Tags sleep

Take millisecond time precision (method i):

Var

T1,t2:int64;

R1:int64;

Begin

t1:=gettickcount;//get start Count WINDOWS API

Sleep (1000); {do ...} Execute the code to be timed

t2:=gettickcount;//Get End Count value

r1:=t2-t1;//get time, Y ' Guo 4~ (}.u_%t "HV unit millisecond (ms)

ShowMessage (IntToStr (R1));

End

Take millisecond time precision (method two):

Use dateutils;//Reference dateutils unit

Var

T1,t2:tdatetime;

R1:int64;

Begin

T1:=now ()//Get Start timer time

Sleep (1000); {do ...} Execute the code to be timed

T2:=now ()//Get End Timer

R1:=secondsbetween (T2,T1)//Get time, d6k=+w TSOUBP fertility _ii unit seconds (s)

R1:=millisecondsbetween (T2,T1)//timing time,

Ftvggu8e36

Unit Millisecond (MS)

ShowMessage (IntToStr (R1));

End

Note: The above two methods are tested by me as if they can only produce 0.01 seconds of timing accuracy.

Take system-level time precision:

Var

C1:int64;

T1,t2:int64;

r1:double;

Begin

QueryPerformanceFrequency (C1);//windows API return Count Frequency (intel86:1193180) (Gets the number of times the system's high-performance frequency counter vibrates within a millisecond)

QueryPerformanceCounter (t1);//windows API get Start meter value

Sleep (1000); {do ...} Execute the code to be timed

QueryPerformanceCounter (T2)//Get End Count value

r1:= (T2-T1)/c1;//gets the time of the timer,

L ':(yo piece}^k (fertile * Soft {Ux

j5^ gz, 1a6t. " 1) Ti

X@t) AC W Teaching

Yjtt]$1ucqs ' E) net 7g per second (s)

r1:= (T2-T1)/c1*1000;//get time, in milliseconds (ms)

r1:= (T2-T1)/c1*1000000;//gets the time of the timer, unit microseconds

ShowMessage (FLOATTOSTR (R1));

End

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.