Discussion:
Real time clock with millisecond resolution <newb>
(too old to reply)
Hunter
2007-08-27 13:15:41 UTC
Permalink
Hi all,

Please forgive me for my newbie question. It's just that I've run out
of options, so I'm posting my question here.
What I need is to implement a time-stamping mechanism, and I need the
time to be in millisecond resolution. I tried my best with the
standard ansi C functions, but the best I could get was a real-time
clock with seconds.
However, I was given a good advice, to try and look for something
system-specific that would provide me with a calendar-time clock with
millisecond resolution and precision.

If anyone knows how I can access calendar time with a resolution as
high as millisecond, and similar precision, I'll appreciate the help.
Please write your answer as simple as possible, as I'm not a
professional programmer, so my C skills are a limited.

My system is a Desktop running Win2000, I have a Visual C++ 6.0

Thank you very much.
Carl Daniel [VC++ MVP]
2007-08-27 14:09:07 UTC
Permalink
Post by Hunter
Hi all,
Please forgive me for my newbie question. It's just that I've run out
of options, so I'm posting my question here.
What I need is to implement a time-stamping mechanism, and I need the
time to be in millisecond resolution. I tried my best with the
standard ansi C functions, but the best I could get was a real-time
clock with seconds.
However, I was given a good advice, to try and look for something
system-specific that would provide me with a calendar-time clock with
millisecond resolution and precision.
If anyone knows how I can access calendar time with a resolution as
high as millisecond, and similar precision, I'll appreciate the help.
Please write your answer as simple as possible, as I'm not a
professional programmer, so my C skills are a limited.
My system is a Desktop running Win2000, I have a Visual C++ 6.0
You can get millisecond resolution using GetSystemTime, but you'll usually
only get about 15ms precision. Using timeBeginPeriod to set the period to
1ms will increase the precision of the clock (but perhaps not quite to 1ms
precision), but at a cost of increased system overhead. You can still
expect to see a "jitter" of up to several milliseconds in the clock times
that your program observes due to scheduling (i.e. your process may not run
for 10's or even 100's of milliseconds at a time, without warning).

-cd
Hunter
2007-08-28 13:39:54 UTC
Permalink
On Aug 27, 4:09 pm, "Carl Daniel [VC++ MVP]"
Post by Carl Daniel [VC++ MVP]
Post by Hunter
Hi all,
Please forgive me for my newbie question. It's just that I've run out
of options, so I'm posting my question here.
What I need is to implement a time-stamping mechanism, and I need the
time to be in millisecond resolution. I tried my best with the
standard ansi C functions, but the best I could get was a real-time
clock with seconds.
However, I was given a good advice, to try and look for something
system-specific that would provide me with a calendar-time clock with
millisecond resolution and precision.
If anyone knows how I can access calendar time with a resolution as
high as millisecond, and similar precision, I'll appreciate the help.
Please write your answer as simple as possible, as I'm not a
professional programmer, so my C skills are a limited.
My system is a Desktop running Win2000, I have a Visual C++ 6.0
You can get millisecond resolution using GetSystemTime, but you'll usually
only get about 15ms precision. Using timeBeginPeriod to set the period to
1ms will increase the precision of the clock (but perhaps not quite to 1ms
precision), but at a cost of increased system overhead. You can still
expect to see a "jitter" of up to several milliseconds in the clock times
that your program observes due to scheduling (i.e. your process may not run
for 10's or even 100's of milliseconds at a time, without warning).
-cd
I'm going to try and use it, thank you very much.

Ben Voigt [C++ MVP]
2007-08-27 19:09:48 UTC
Permalink
Post by Hunter
Hi all,
Please forgive me for my newbie question. It's just that I've run out
of options, so I'm posting my question here.
What I need is to implement a time-stamping mechanism, and I need the
time to be in millisecond resolution. I tried my best with the
standard ansi C functions, but the best I could get was a real-time
clock with seconds.
However, I was given a good advice, to try and look for something
system-specific that would provide me with a calendar-time clock with
millisecond resolution and precision.
If anyone knows how I can access calendar time with a resolution as
high as millisecond, and similar precision, I'll appreciate the help.
Please write your answer as simple as possible, as I'm not a
professional programmer, so my C skills are a limited.
My system is a Desktop running Win2000, I have a Visual C++ 6.0
I implemented a Windows version of gettimeofday. What I did was to get the
current system time (to the nearest second) and also call
QueryPerformanceCounter. Then I arbitrarily choose that the milliseconds
are zero at that time. Later, calling QueryPerformanceCounter again gives
you very high resolution elapsed time, which you can add to your original
system time to get a high resolution calendar time.

The only drawback with that is that the extra resolution isn't shared
between processes. But for most cases, like if you just need extra
precision in your debug log, it's great. Computer time can't be expected to
be synchronized to any external source with better than +/- 1 second anyway.
Post by Hunter
Thank you very much.
Loading...