Re: Constructing a more precise "Now()" and "DateDiff()"

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Bob O`Bob" <filterbob@xxxxxxxxxxxxxxx> wrote in message news:Ovl1F2ybGHA.4900@xxxxxxxxxxxxxxxxxxxxxxx

In my immediate project, precision is far more important than
accuracy. What's happening is I'm writing a lot of small files
(email messages) and I want some way of distinguishing them
in sequence.

As Jerry has already said, perhaps you might consider simply using your own sequence number. However, if you actually do want to use a "timestamp" and if the resolution of my own suggested method of using GetLocalTime is not sufficient for you then why not just record the actual time (using GetLocalTime or GetSystemTime, whichever best suits your purpose) at the start of your program. At the same time you should record the value returned by the timeGetTime function. Then every time you want to know the current time you simply use the timeGetTime function and deduct from its returned value the initial value that you got from timeGetTime at the start of your program. That will give you a specific number of milliseconds to add to the initially recorded time stamp. Writing a function to do that in a properly formatted way should be very easy. The resolution of timeGetTime is one millisecond (provided you use the timeBeginPeriod API to set the resolution to one millisecond at the start of your program). The only "catch" you would have to watch out for is that the value returned by timeGetTime will wrap if your machine has been permanently on for about 49 days, and will begin to "go negative" as far as VB is concerned after about 24 days, but if that might be a problem for you it is of course very easy to code around.

Otherwise, if the one millisecond resolution of timeGetTime is still not sufficient for your purposes you can instead use QueryPerformanceCounter (in association with QueryPerformanceFrequency) in a similar way. The resolution of QueryPerformanceCounter is machine dependent, but it is always very high and you can virtually guarantee meaningful results for resolutions of the order of about a hundredth of a millisecond. There is a little very rare "glitch" problem with QueryPerformanceCounter (which I think other people have mentioned) but for your specific purposes you won't need to worry about it. You'd have to check that QueryPerformanceCounter was actually available on whatever machine your code is running on of course, but you can do that simnply by calling it and testing for a zero result. Having said that, I don't think there can be many machines about these days on which QueryPerformanceCounter will not work.

Mike





--

.



Relevant Pages

  • Re: alternative to gettickcount
    ... m_StartTime = timeGetTime() ... It's almost an "industry standard" and I'm sure it is what many people do, even in the kitchen when they are timing baking periods for steak and kidney pies!, but it will NOT cut the error in half, as you have suggested. ... There is no way that you can say you have a result to a resolution of within half a millisecond on a one millisecond timer using that technique! ... It will avoid unneccessarily generating errors larger than they really need to be, but it will not improve the overall resolution of the basic timing device! ...
    (microsoft.public.vb.general.discussion)
  • Re: How to write out current time including milliseconds?
    ... TimeGetTime gives you a resolution of 1 millisecond, provided you use timeBeginPeriod 1 before you start using it, and it returns the number of milliseconds since Windows was last started. ...
    (microsoft.public.vb.general.discussion)
  • Re: WaitForSingleObject timeout not working
    ... GetTickCount on your system has 15-16 ms resolution. ... timeGetTime(). ... > When using WaitForSingleObject and/or Win32 Sleep function the thread ...
    (microsoft.public.win32.programmer.kernel)
  • Re: now.ticks does not work
    ... Using it to time code that is faster then the quantum that is used ... For more details on the Precision & Resolution of Now.Ticks, ... | i will check out your QueryPerformanceCounter ...
    (microsoft.public.dotnet.languages.vb)
  • Re: I need to get the lapsed time in the execution of various functions...
    ... If you want timings with a greater resolution you can use the timeGetTime API, which has a resolution of 1 millisecond provided that you use the timeBeginPeriod API to set it to 1 millisecond before using it. ... On most machines this is capable of giving you timings with a resolution of about 1 microsecond (although there are some "catches" to be aware of where under certain unusual circumstances the QueryPerformanceCounter API can "jump" by a significant amount. ...
    (microsoft.public.vb.general.discussion)