Re: what is the fast(est) way to calculate millisec time span in mfc

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Use the QueryPerformanceCounter timer. The millisecond timer is updated only every 15ms.
So even using GetTickCount (which nominally returns values in ms) is not going to give you
resolution at the ms level (66 ticks/second).

In addition, be aware that hundreds of milliseconds can elapse between the packet being
received and your program being activated. I'm not sure why you can't do it in the
device, because 10KB/s is dead slow. You could used compressed encodings to reduce
bandwidth requirements, e.g., 0..127ms encoded in one byte, 255.16383ms encoded in 2
bytes,

0xxxxxxxx 0..127 ms
1xxxxxxxx 0 yyyyyyy 128..16383ms
1xxxxxxxx 1 yyyyyyy 0 zzzzzzz 16384..1048575ms (~17min)

etc.

Or, alternatively, if you don't need precision for larger time values, try using just a
single byte:

0xxxxxxx 0..127ms
10yyyyyy 128ms..+n*yyyyyyms for some value of n of lower resolution,
n=5 = 128..443ms
11yyyyyy 444ms..m*yyyyyyms for some value m of lower resolution
m=10 = 444..1074ms

and so on. So you can see that there are some intertesting compressions possible. What
you're doing here is dealing with percentage error; at larger time intervals, the
percentage error is not excessive.

If you need precision timestamps and you are writing your own device driver, consider
using something like DeviceIoControl instead of ReadFile, and make the first 64 bits be
the KeQueryPerformanceCounter value at interrupt time. Always be aware that using the QPC
values can have skew between processors on a multiprocessor system.

But timestamping things at the user level is going to give you truly massive jitter; any
correlation between the timestamp you assign and the actual time of the data being
received is problematic.
joe

On Thu, 1 Feb 2007 09:31:02 -0800, ElectronicMan <ElectronicMan@xxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:

hello board, I have an application that receives about 10 Kbytes/Sec from
serial port in 8 byte packets. packets have to be labelled with time. I
couldn't add time label within device because it would need much wider
communication bandwidth, so windows has to do it. I ususally write my
programs using mfc. Now I want to add a time label to each packet with 1
millisec resolution. I don't want to add a timer because handling timer
messages need an unnecessay computational overhead. I like to save last time
label and when new packet arrived I calculate elapsed time and add the
elapsed time to previous label and dispatch packet. CTime and CTimeSpan
resolution are limited to 1Sec. Can someone give me some suggestions please?
any suggestion is welcomed.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • 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)
  • RE: WaitForSingleObject timeout not working
    ... Do you mean that the high-resolution timer will also get the same problem ... This is a problem with the *assumption* about GetTickCount(). ... counter is not updated frequently enough to provide accurate millisecond ... resolution. ...
    (microsoft.public.win32.programmer.kernel)
  • 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: One-shot high-resolution POSIX timer periodically late
    ... small bursts due to network jitter (typical average rate of 1000 packets ... Just before I re-send a packet, I arm a one-shot timer in order to ... Linux Plug and Play Support v0.97 Adam Belay ...
    (Linux-Kernel)
  • Re: [patch 00/21] hrtimer - High-resolution timer subsystem
    ... >> This way the list head is only necessary for the high resolution case. ... > (e.g. wakeup + timer restart). ... Even if the clock ...
    (Linux-Kernel)