Re: Need to implement a calendar clock with millisecond resolution, please help.



On Aug 26, 2:38 am, Hunter <Igal.Hun...@xxxxxxxxx> wrote:
Hi all,

I know it may sound like dump newbie question (which is very much
true, as I am a newbie, not even a real programmer), but I need to
implement a calendar time clock with a millisecond resolution. For
hours I've been digging the Visual C++ 6 help and Googling like a
madman. It seems to me that the standard C just don't have the tool to
get this type on information. Sure I found the time() and the clock()
but they don't really help me.

Is there any way I can get a real calendar time, say from a system?
Maybe I should look in the direction of some kind of Assemly language
routine inserted into the C (not that I like the option, last time I
touched Assembly was years ago, back at the university).

I'll appreciate any ideas you have.

/* I run VC6, on a PC powered by Win2000 */

Your question is a FAQ:
19.37: How can I implement a delay, or time a user's response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(),
and
usleep(). (A function called wait(), however, is at least
under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed
execution
time, and may even have subsecond resolution, if
CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor
time
used by the current program, which on a multitasking system
may
differ considerably from real time.

If you're trying to implement a delay and all you have
available
is a time-reporting function, you can implement a CPU-
intensive
busy-wait, but this is only an option on a single-user,
single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it's tempting to use a do-nothing
loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working
properly
next month when a faster processor comes out. Perhaps worse,
a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp.
197-8,215-
6; POSIX Sec. 4.5.2.

Under Visual C++ the highest resolution is found using the high
resolution timer (which may or may not exist).
To find out more about that, try a visual C++ programming group on the
microsoft news server.

You can probably find code already written to do what you want on
sourceforge or with a google search.
I find it hard to believe you looked already and did not turn anything
up.

.



Relevant Pages

  • Re: Timing function in C
    ... resolution up to a millisecond, ... System V and POSIX. ... only clock() is part of the ANSI Standard. ... If you're trying to implement a delay and all you have available ...
    (comp.lang.c)
  • Re: Tuning suggestions, please?
    ... trying to tune the performance and I can't track down a 15ms delay. ... I don't think it's a 15ms delay, rather it's the resolution of the clock. ... University of Leicester, Leicester, LE1 7RH, UK ...
    (comp.lang.java.programmer)
  • Re: Timespec for DCM outputs (Spartan 3) ?
    ... resolution of 130ps step sizes for programmed delay. ... One thing to watch, though, is the jitter on the clock input line. ... > Each flip flop is clocked from a different DCM. ...
    (comp.arch.fpga)
  • Re: [patch 00/21] hrtimer - High-resolution timer subsystem
    ... Right now the primary function of the state is to tell whether the timer ... > Of course if you consider the possibility of including high resolution ... problem requires solving problems in the clock abstraction first, ...
    (Linux-Kernel)
  • Re: Re basic to vb
    ... With the delay set at .250 the motor steps really slow, ... If you're using the delay code that uses the VB Timer function then the smallest average delay you are going to get is about 0.015 seconds. ... If you want a higher resolution then you can use the multimedia timer, either polling directly in a code loop as you are currently doing or alternatively using the events of a high resolution Timer based on the multimedia timer. ... For a code loop based solution such as the method you are currently using it is a simple case of changing your time variables from Singles to Longs and calling the timeGetTime function, which returns a value in milliseconds and which is capable of a resolution of one millisecond. ...
    (microsoft.public.vb.general.discussion)