Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need to implement a calendar clock with millisecond resolution, please help.

Reply
Thread Tools

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

 
 
Hunter
Guest
Posts: n/a
 
      08-26-2007
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 */

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      08-26-2007
In article <(E-Mail Removed) .com>,
Hunter <(E-Mail Removed)> 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.


You are correct, standard C does not have such a mechanism.

You probably don't need to drop into assembler, though; check out
a Windows programming newsgroup to find out what you can do within
the Windows API (since assembler wouldn't be portable anyhow.)

By the way, after you get finished figuring out how to get
millisecond -resoluti0n-, you are going to have the even more
fun task of getting similar -precision-. On most systems, the
built-in real-time clocks drift, fairly noticably, so if you want
calendar time with millisecond resolution, you are going to need
to start thinking about how to keep that clock synchronized with
some sort of standard time signal.
--
Prototypes are supertypes of their clones. -- maplesoft
 
Reply With Quote
 
 
 
 
Hunter
Guest
Posts: n/a
 
      08-26-2007
Oh... API... Whatever that means
As for precision, do you know how much it can "drift"?
I mean, what I need is to synchronize between to programs, each
acquiring it's data from a different source (And the data rate is
about 1kHz), and saving it into the file. The programs not necessarily
start at the same time, but they do run simultaneously most of the
time. So I thought of "time stamping" the data in files.
Thank you very much!

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      08-26-2007
On Sun, 26 Aug 2007 03:07:23 -0700, Hunter wrote:

> Oh... API... Whatever that means
> As for precision, do you know how much it can "drift"?

Usually the rate of hardware clocks is not extremely accurate, it
can be slightly too fast or too slow, depending among other things
on temperature. Normally the differences are of about one part on
10**5 or less, but if you need to measure times to within one
microseconds it will make the least significant digits bogus.

> I mean, what I need is to synchronize between to programs, each
> acquiring it's data from a different source (And the data rate is
> about 1kHz), and saving it into the file. The programs not necessarily
> start at the same time, but they do run simultaneously most of the
> time. So I thought of "time stamping" the data in files.

Well, so you just need high precision and resolution, even if
accuracy is not perfect.

--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

 
Reply With Quote
 
Hunter
Guest
Posts: n/a
 
      08-26-2007
Such a drift strikes me as a bit odd. I meen the sysem clock runs at
hundreds of MHz, and probably has plenty of PLLs and dividors to form
all the derivative clocks.

Anyway thanks a lot!

 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      08-27-2007
On Aug 26, 2:38 am, Hunter <(E-Mail Removed)> 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.

 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      09-09-2007
On Sun, 26 Aug 2007 03:07:23 -0700, Hunter <(E-Mail Removed)>
wrote:

> Oh... API... Whatever that means


http://en.wikipedia.org/wiki/API and in this case particularly
http://en.wikipedia.org/wiki/Windows_API

> As for precision, do you know how much it can "drift"?

s/precision/accuracy/; see PP (previous post)

> I mean, what I need is to synchronize between to programs, each
> acquiring it's data from a different source (And the data rate is
> about 1kHz), and saving it into the file. The programs not necessarily
> start at the same time, but they do run simultaneously most of the
> time. So I thought of "time stamping" the data in files.


Programs on the same machine, or different machines?

On the same machine, if that machine can be a multiprocessor -- and
any machine you bought recently in the consumer market likely was --
make sure you use a call which gets time from a 'global' or 'shared'
clock, not a per-CPU counter, since those may not be or stay in sync.
I'm confident Windows does provide such a capability, but I'm not sure
if (or when) it's the default, and if not how you get it, and since
it's implementation-specific (to Windows) it's offtopic here.
comp.programmer.ms-windows.win32 is rumored to be good.

On different machines, if you want to keep clocks in sync to the
millisecond level, you need something specialized for the purpose.
There may be some Windows-specific capability, though I haven't heard
of one; the win32 group would probably know. There is an Internet
standard (in both the de-jure and de-facto sense) for this called NTP
(Network Time Protocol), but last I looked they weren't getting very
good precision on Windows. You could try comp.protocols.time.ntp ,
and in particular www.meinberg.de reportedly supports (free) a build
and configuration for Windows from the generic/portable NTP source.
(The company's business is selling precision clock _hardware_, and
they provide this support for NTP free presumably in the hope that
lots more NTP users = somewhat more clock buyers.)

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
pedals pedals is offline
Junior Member
Join Date: Sep 2011
Posts: 1
 
      09-12-2011
It should be possible to get millisecond timing in assembler too? But how? No other forum has helped.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Millisecond timestamp function? Joshua J. Kugler Python 0 10-19-2007 05:15 PM
UTC time in millisecond palmis Java 26 01-19-2006 07:38 AM
timer with millisecond precision WeAreBorg C Programming 2 09-29-2005 12:27 AM
How to sleep during millisecond Yeounkun, Oh C Programming 11 12-14-2004 02:29 PM
millisecond delay in C - linux Macgyver C Programming 8 01-07-2004 12:28 PM



Advertisments