How accurate is the Delphi "Now" timestamp

I will be working on a project that will require (fairly) accurate input of the time series of RS232 serial and network data from custom equipment. Since the data will come from a number of independent hardware sources, I will need to match all the data so that they can be linked / interpolated to the nominal point in time.

My direct one, although it was easy to use the built-in Now command to timestamp, however a quick Google seems to indicate that it will only be accurate to 50 ms or so.

Unfortunately, the more I read, the more I get confused. There seem to be a lot of conflicting tips on GetTickCount and QueryPerformanceCounter, with the complications of today's multi-core processors and processor throttling. I also saw posts recommending using Windows multimedia timers, but I can not find the code snippets for this.

So, someone can advise me:

1) How accurate is the Now.

2) Is there a simple, higher accuracy.

Note. I would hope for a timestamp within, say, 10 milliseconds, and I am not looking for a timer as such, but simply the best method for stamping time. This will run on Windows 7 32-bit low-power mini-PC. I will use Delphi XE or Delphi 2007 if that matters.

+6
source share
1 answer

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

Although TDateTime values ​​may represent milliseconds, it is now accurate only to the nearest second.

Despite this, looking at the current implementation is now as accurate as the GetLocalTime API.

Performing a quick test, it shows that it now returns values ​​for every millisecond in hours, for example:

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

When I project1 >times.txt this console program from the command line project1 >times.txt , on a machine with a 64-bit version of Windows 7, I got a file that lasts 29 milliseconds continuously (there is no one in the file).

You should be faced with the fact that when working in a Windows environment, your application / thread can receive processor fragments with different time intervals depending on how busy the system is and the priority of your application / threads compared to all other threads running in the system.

+10
source

All Articles