All of the delphi 5 time functions use the windows LocalTime function
which always seems to return the milliseconds to the nearest tenth (IE.
540, 670).
Anyone have any ideas.
Richard Hilton
Do you mean accuracy or resolution? Millisecond accuracy may take a
radio tuned to WWVB or the like with some diurnal corrections.
Millisecond resolution is easier. There is the PerformanceCounter and
the CPU ClockCycleCounter.
Torjei Kvinen <t-k...@online.no> in a post on about 02/24/00 included
the following which gives some information on both.
Try the TimeStampCounter (introduced on Intel Pentium), it's a timer
running at CPU speed.
function RDTSC : Int64;
asm
dw $310F // opcode for RDTSC
end;
function CPUSpeed : Longword;
var
f,tsc,pc,pc2 : Int64;
begin
if QueryPerformanceFrequency(f) then
begin
QueryPerformanceCounter(pc);
tsc := RDTSC;
Sleep(0);
QueryPerformanceCounter(pc);
tsc := RDTSC;
Sleep(100);
QueryPerformanceCounter(pc2);
tsc := RDTSC-tsc;
pc := pc2-pc;
result := round(tsc*f/(pc*1e6));
end
else
result := 0;
end;
You mean, presumably, the nearest ten - it is probably reliant on DOS
int 21/2C, which returns h m s centiseconds. Further observation will
in that case reveal that it is not the *nearest* ten, since the number
goes up in steps of 50 or 60 ms. You are actually, I expect, observing
the 18.2Hz clock, at $40:$6C. Read my Web page pas-time.htm, and Kris
Heidenstrom's The Timing FAQ, which my page links to or towards.
You cannot get the current time that accurately with a standard PC.
There is no means of setting it with millisecond accuracy; and the
uncertainty in the crystal frequency will give a rate error of over a
millisecond in an hour. If you want that accuracy you will need to link
to a precision time source, or install an MSF (or local equivalent; you
give no trustworthy indication of your location) receiver and correct
for propagation time (a millisecond per megafoot, more or less).
Even if you are only interested in time differences, the internal
frequency will not be that good over many minutes; though it might help
to run the PC off a regulated supply in a constant temperature
environment.
Only if it is merely millisecond resolution that you need do you have
any real chance of success using just a standard PC.
The RDTSC instruction gives ample resolution; but it is derived from the
CPU clock, and there is no design need for that to be particularly
accurate or stable : remember that a millisecond in a quarter of an hour
is about one part per million.
The best time source available to you is probably the RTC clock, since
it is designed to keep time long-term. You should be able to use the
interrupt capability of the RTC, which includes IIRC 1Hz, to call a
routine once every second which reads RDTSC and keeps the last few
values. If you also read RDTSC at your event-to-be-timed, then you can
use a few recent and future RTC-controlled RDTSC readings to calibrate
the RDTSC scale for that epoch.
But, of course, a Delphi system is (at present) a Win 95 (or worse)
system; and your software may be interrupted by other processes which
may make the whole idea of millisecond resolution rather meaningless -
though if you wand to time an external event, it may be possible to
cause it to snapshot RDTSC with an acceptable uncertainty in delay.
If, though, you are only interested in comparing speeds of rival pieces
of software, then RDTSC is ideal, since it has the same perturbations as
the rest of the CPU.
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper 4-line sig. separator is as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
If you're looking for time of day with (near) millisecond response (not 55mS
tick rate), then use timeGetTime.
William Meyer
"Matt Gilson" <ma...@hdsoftware.com> wrote in message
news:38FF8EDE...@hdsoftware.com...
> I need to get the current time (accurately) down to the millisecond.
>
> All of the delphi 5 time functions use the windows LocalTime function
> which always seems to return the milliseconds to the nearest tenth (IE.
> 540, 670).
>