Solved

Better timer than WIN32 GetLocalTime()

Posted on 2006-06-13
5
2,262 Views
Last Modified: 2013-12-03
I'm logging some incoming data that needs to be time correlated and was using GetLocalTime() to get a SYSTEMTIME value for each incoming packet.  Unfortunately at times multiple (usually 2 or 3) packets come in and get the same SYSTEMTIME value.  I'm sure this is due to the resolution of the timer.  The specs on GetLocalTime() say it resolved to mS but it's pretty clear that it doesn't really provide 1mS resolution.

I think I read somewhere about a better timer mechanism on Windows.  I did find some stuff on using the CPU counter register but this is more useful for providing an accurate RELATIVE time.  I need an absolute time.

Any ideas?
0
Comment
Question by:jhance
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 100 total points
ID: 16900058
I know two ways to calculate exact time:
1) Using QueryPerformanceFrequency and QueryPerformanceCounter - this can be used to calculate time intervals.
2) Multimedia timer - see timeGetTime and other functions.

Both ways don't provide absolute time. However, you can get absolute start time in the beginning of the program, and add time interval to it for every packet. Pseudo code:
absoluteStartTime = GetLocalTime();
windowsStartTime =  timeGetTime();

For every packet:
packetTime = absoluteStartTime + (timeGetTime() - windowsStartTime);
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
ID: 16904670
If you actually don't need a better precision but simply a unique and increasing filetime, I would recommend to save the last time stored and add a millisecond if there is no difference when taking the next time. I don't think that any other filetimes you have are much more precise.

Regards, Alex


0
 
LVL 7

Assisted Solution

by:aib_42
aib_42 earned 50 total points
ID: 16913274
If you use any kind of system timer, you are bound to get duplicate times due to their resolution. To get unique values, you want a timer with resolution equal to or a small multiple of your clock speed, for which the performance counter should suffice (if the target computer has one).

If your algorithm needs absolute time, though, how come duplicate values are inacceptable?
I'd look for another solution such as adding a "sequence number" of sorts to distinguish among identical SYSTEMTIME values, or do what Alex said.
0
 
LVL 32

Author Comment

by:jhance
ID: 16913914
Actually I'm trying to correlate network packets that are being logged and timestamped by a tool called Analyzer, which is based on the WinPCap driver.  It timestamps the packets with what appears to be uS resolution where I'm not even getting mS with GetLocalTime().

Since it's open source I've been looking through the code and it uses a thing called wxDateTime from the wxWidgets library.  That's also open source but I've been unable to figure out how/where they get their time on a Windows system.  I don't know where open source code writers learn how to write convoluted code but I'm never able to follow what they do.

Anyway, if anyone has any experience with wxWidgets and can point me to where or how they do the wxDateTime it would help (I think...)
0
 
LVL 8

Assisted Solution

by:craigwardman
craigwardman earned 50 total points
ID: 16988252
For an x86 specific solution, you could try the mnemonic instruction 'RDTSC' which reads the time stamp from the CPU..

A c++ inline example:


unsigned long hTime, lTime;

  __asm{
      RDTSC
      mov hTime, EDX
      mov lTime, EAX
}


Some info on this:
The time-stamp counter is contained in a 64-bit MSR. The high-order 32 bits of the MSR are loaded into the EDX register, and the low-order 32 bits are loaded into the EAX register. The processor increments the time-stamp counter MSR every clock cycle and resets it to 0 whenever the processor is reset.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question