1

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

6
  • What do you mean by "get time" and "compute time". Please can you be precise. Commented May 28, 2014 at 14:06
  • RDTSC can give you most precise value you can achieve. Maybe look this way? en.wikipedia.org/wiki/Time_Stamp_Counter Commented May 28, 2014 at 14:26
  • microsecond and millisecond are two entirely different scales of time. Commented May 28, 2014 at 14:36
  • 2
    @JerryDodge Hence the question Commented May 28, 2014 at 15:49
  • @David Yes, and I hate it when people delete their comments making mine look bad... Commented May 28, 2014 at 20:16

2 Answers 2

6

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;
Sign up to request clarification or add additional context in comments.

2 Comments

In Delphi you would use TStopwatch for a platform independent, OOP way to access high perf timers.
Yes, as long as you're using Delphi XE (I think) or newer. Since the question didn't specify which Delphi version is being used, I gave a more "universal" answer.
4

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.