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

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
user3226824
  • 47
  • 1
  • 6
  • What do you mean by "get time" and "compute time". Please can you be precise. – David Heffernan May 28 '14 at 14:06
  • RDTSC can give you most precise value you can achieve. Maybe look this way? http://en.wikipedia.org/wiki/Time_Stamp_Counter – Darthman May 28 '14 at 14:26
  • microsecond and millisecond are two entirely different scales of time. – Jerry Dodge May 28 '14 at 14:36
  • 2
    @JerryDodge Hence the question – David Heffernan May 28 '14 at 15:49
  • @David Yes, and I hate it when people delete their comments making mine look bad... – Jerry Dodge May 28 '14 at 20:16
  • @Jerry, and I hate when people delete questions on whose people spend some time. Then they suddenly [`"come" up`](http://stackoverflow.com/questions/23482468/npm-failing-right-after-installing-node-js/23496004#comment36031364_23482468) with their own solution to a related question... Just deal with it ;-) – TLama May 28 '14 at 21:24

2 Answers2

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;
jthurman
  • 465
  • 7
  • 13
  • In Delphi you would use `TStopwatch` for a platform independent, OOP way to access high perf timers. – David Heffernan May 28 '14 at 14:53
  • 3
    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. – jthurman May 28 '14 at 14:57
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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Kanitatlan
  • 395
  • 1
  • 9