Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How accurate is Delphi's 'Now' timestamp

I'm going to be working on a project that will require (fairly) accurate time-stamping of incoming RS232 serial and network data from custom hardware. As the data will be coming from a number of independant hardware sources, I will need to timestamp all data so it can be deskewed/ interpolated to a nominal point in time.

My immediate though was just to use the inbuilt Now command to timestamp, however a quick Google seems to indicate that this is only going to be accurate to around 50 msecs or so.

Unfortunately, the more I read the more confused I become. There seems to be a lot of conflicting advice on GetTickCount and QueryPerformanceCounter, with complications due to todays multicore processors and CPU throttling. I have also seen posts recommending using the Windows multimedia timers, but I cannot seem to find any code snippets to do this.

So, can anyone advise me:

1) How accurate 'Now' will be.

2) Whether there is a simple, higher accuracy alternative.

Note: I would be hoping to timestamp to within, say , 10 milliseconds, and i am not looking for a timer as such, just a better time-stamping method. This will be running on a Windows 7 32 bit low-power micro-PC. I will be using either Delphi XE or Delphi 2007, if it makes any difference.

like image 574
HMcG Avatar asked Jan 29 '13 00:01

HMcG


1 Answers

According to documentation, Now is as accurate only to the nearest second:

Although TDateTime values can represent milliseconds, Now is accurate only to the nearest second.

Despite this, looking at the current implementation, Now is as accurate as the GetLocalTime windows API could be.

Making a quick test, it shows Now returns values for each millisecond in the clock, for example:

begin
  System.SysUtils.FormatSettings.LongTimeFormat := 'hh:mm:ss.zzz';
  for I := 1 to 5000 do
    Writeln(TimeToStr(Now()));
end.

When I executed this console program from the command line project1 >times.txt, in a Windows 7 64 bits machine, I got a file that goes along 29 milliseconds continually (no one is missing in the file).

You have to face the fact that running in a Windows environment, your application/thread may get processor slices with varying time in between, depending on how busy is the system and the priority of your application/threads versus all the other threads running in the system.

like image 196
jachguate Avatar answered Nov 15 '22 06:11

jachguate