I work on delphi language and I need to compute time for process in microsecond.
I can compute time in hour minute second and millisecond, is there any function for compute time in microsecond in delphi language??
I work on delphi language and I need to compute time for process in microsecond.
I can compute time in hour minute second and millisecond, is there any function for compute time in microsecond in delphi language??
There is a CPU-dependent "high resolution performance counter" that you can access with the QueryPerformanceCounter()
API call.
QueryPerformanceCounter()
gives you the value of that performance counter. (Historically, the performance counter was simply a count of CPU cycles, but hyper-threading and multi-core CPUs have made that unreliable, so now it just measures some very small interval of time, < 1us)
To find out what this unit of time is, use QueryPerformanceFrequency()
. This gives you the number of high-resolution performance counter units per second. To get the number per microsecond, divide by 1000000. On my Sandy Bridge i7, it's about 35 units per microsecond.
Some code:
Using QueryPerformanceCounter
to measure the execution time of some code:
var
StartTime, EndTime, Delta: Int64;
begin
QueryPerformanceCounter(StartTime);
//Code you want to measure here
QueryPerformanceCounter(EndTime);
Delta := EndTime - StartTime;
//Show Delta so you know the elapsed time
end;
Using QueryPerformanceFrequency
to find out how many high-resolution units are in a microsecond:
var
Frequency, UnitsPerMS: Int64;
begin
QueryPerformanceFrequency(Frequency);
UnitsPerMS := Frequency div 1000000;
end;
It sounds to me like what you actually want to use is the TStopWatch
type. This provides a Delphi wrapper for QueryPerformanceCounter()
and related APIs and hides all the details from you. This is in the System.Diagnostics
unit and is available from Delphi 2010.