Hid*_*den 16 delphi time function delphi-xe3
我想知道如何计算Delphi中函数的消耗时间.
然后我想显示使用的时间并将其与另一个函数或组件进行比较,以便了解更快的函数.
Dav*_*nan 53
您可以使用系统的高分辨率性能计数器使用TStopwatch本System.Diagnostics机测量经过的时间.
var
Stopwatch: TStopwatch;
Elapsed: TTimeSpan;
....
Stopwatch := TStopwatch.StartNew;
DoSomething;
Elapsed := Stopwatch.Elapsed;
Run Code Online (Sandbox Code Playgroud)
要以秒为单位读取时间值(例如,从时间跨度),请执行以下操作:
var
Seconds: Double;
....
Seconds := Elapsed.TotalSeconds;
Run Code Online (Sandbox Code Playgroud)
And*_*and 21
你可以使用QueryPerformanceCounter和QueryPerformanceFrequency功能:
var
c1, c2, f: Int64;
begin
QueryPerformanceFrequency(f);
QueryPerformanceCounter(c1);
DoSomething;
QueryPerformanceCounter(c2);
// Now (c2-c1)/f is the duration in secs of DoSomething
Run Code Online (Sandbox Code Playgroud)