Re: now.ticks does not work

Tech-Archive recommends: Fix windows errors by optimizing your registry



Willie,
| the problem i have is that the now.ticks is not relayable.
I agree: Using it to time code that is faster then the quantum that is used
to update it is not very "reliable". However! I consider Now.Ticks to be a
very reliable number!

Read my & Armin's message again, it (Now.Ticks) is very reliable as it will
be updated every quantum based on the OS, where quantum is OS based (see
first link below). However it is not very precise, because the quantum used
to update it is relatively course.

In other works Now.Ticks is reliably updated approximately every .01 seconds
as Armin suggests (as its based on the OS "clock"), however you are trying
to time something that only lasts .003 seconds. Simple math suggests that
..003 goes into .01 3 & 1/3 times. If you were timing something that took
seconds or minutes, then Now.Ticks would be very reliable!

For more details on the Precision & Resolution of Now.Ticks, see:

"Precision" of DateTime (100-nanoseconds):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclasstopic.asp

"Resolution" of DateTime.Now (.01 seconds):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDateTimeClassNowTopic.asp

Where "Precision" is how many digits of "time" a DateTime contains, while
"Resolution" is how accurate those digits are when using DateTime.Now.

Hope this helps
Jay

"Willie jan" <unknown@xxxxxxxxxx> wrote in message
news:4304485d$0$20022$dbd49001@xxxxxxxxxxxxxxxxxx
| the problem i have is that the now.ticks is not relayable.
| the first click returns 0, the second returns 2.6ms
| i will check out your QueryPerformanceCounter
|
| thanks.
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@xxxxxxx> schreef in
bericht
| news:O62w1tzoFHA.2580@xxxxxxxxxxxxxxxxxxxxxxx
| > Willie,
| > As the others suggest, you should use Long or DateTime to store T.
| >
| > DateTime has a "resolution" of 100-nanoseconds, its precession can vary,
| > in
| > that although it is based on units of 100-nanosecond its resolution or
| > precision is based on the system timer (which may be as course as .01
| > seconds)
| >
| > QueryPerformanceCounter has a resolution of nanoseconds or smaller (its
| > precession is the QueryPerformanceFrequency value). Its based on a
| > "high-resolution performance counter", which I understand is a special
| > hard
| > ware counter on some CPUs...
| >
| > Tickcount has a resolution of microseconds.
| >
| > There is an MSDN Magazine article that discusses timing blocks of code,
| > however I am having trouble finding it right now, it may be the third
link
| > below, however it doesn't feel right...
| >
| > Use QueryPerformanceCounter to Time code in VB.NET:
| > http://support.microsoft.com/default.aspx?scid=kb;en-us;306978
| >
| > Not sure if the following will help or not:
| >
| > Comparing the Timer Classes in the .NET Framework Class Library
| > http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx
| >
| > Implement a Continuously Updating, High_Resolution Time Provider for
| > Windows
| >
http://msdn.microsoft.com/msdnmag/issues/04/03/HighResolutionTimer/default.aspx
| >
| >
| > I normally use QueryPerformanceCounter as its in the units used by
| > Performance Counters:
| >
| > Something like:
| >
| > Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef
counter
| > As Long) As Boolean
| > Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef
| > frequency As Long) As Boolean
| >
| >
| > ' set some time var
| > Dim start, finish, frequency As Long
| > QueryPerformanceFrequency(frequency)
| > QueryPerformanceCounter(start)
| >
| > ' work
| >
| > QueryPerformanceCounter(finish)
| > Dim time As TimeSpan = TimeSpan.FromSeconds((finish - start) /
| > frequency)
| >
| > Alternatively you can use DateTime:
| >
| > ' set some time var
| > Dim start, finish As DateTime
| > start = DateTime.Now
| >
| > ' work
| >
| > finish = DateTime.Now
| > Dim time As TimeSpan = finish.Subtract(start)
| >
| > A third alternative would be to use "Ticks"
| >
| > ' set some time var
| > Dim start, finish As Integer
| > start = Environment.TickCount
| >
| > ' work
| >
| > ' set second time var and comapre to get result
| > finish = Environment.TickCount
| > Dim time As TimeSpan = TimeSpan.FromMilliseconds(finish - start)
| >
| > My understanding is that QueryPerformanceCounter will normally be a
higher
| > resolution then Environment.TickCount, however QueryPerformanceCounter
may
| > not be available.
| >
| > VB.NET 2005 (aka Whidbey, due out later in 2005) simplifies the choice
by
| > providing a System.Diagnostics.Stopwatch class that will automatically
| > choose between QueryPerformanceCounter & Environment.TickCount...
| >
| > http://lab.msdn.microsoft.com/vs2005/
| >
| > http://msdn2.microsoft.com/library/ebf7z0sw.aspx
| >
| > Hope this helps
| > Jay
| >
| >
| > "Willie jan" <unknown@xxxxxxxxxx> wrote in message
| > news:43033dea$0$31942$dbd49001@xxxxxxxxxxxxxxxxxx
| > | place this behind a button that fills a listbox.
| > | as you will see the time is now and then 0 or filled in????????????
| > | by hitting the button.
| > |
| > | is there a way to determine the real elapsed time?
| > |
| > | thanks, Willie
| > |
| > | Dim T As Double
| > |
| > | T = Now.Ticks
| > |
| > | System.Threading.Thread.Sleep(3)
| > |
| > | T = Now.Ticks - T
| > |
| > | ListBox1.Items.Insert(0, T.ToString("0000000000000000"))
| > |
| > |
| >
| >
|
|


.


Quantcast