Solved

Better timer than WIN32 GetLocalTime()

Posted on 2006-06-13
5
2,238 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
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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