Re: what is the fast(est) way to calculate millisec time span in mfc
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 01 Feb 2007 13:46:55 -0500
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 fromJoseph M. Newcomer [MVP]
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.
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: what is the fast(est) way to calculate millisec time span in m
- From: ElectronicMan
- Re: what is the fast(est) way to calculate millisec time span in m
- Prev by Date: Re: Is #include inside stdafx.h a bad idea?
- Next by Date: Re: macros and preprocessor
- Previous by thread: Re: what is the fast(est) way to calculate millisec time span in mfc
- Next by thread: Re: what is the fast(est) way to calculate millisec time span in m
- Index(es):
Relevant Pages
|