Solved

# time in vb.net

Posted on 2004-11-23
1,740 Views
I just want to know what is the minimum amount of time difference we can measure by using vb.net
its like i send a packet and then receive it.
i want to calculate the time difference. i guess it mightl be 1 millisecond or lower.

is their any way to get the time difference less than 1 milli second

---prashanth
0
Question by:prashanth_gurijala

LVL 28

Assisted Solution

I think that minimum is a tick

Dim T1, T2 As System.DateTime

T1 = Now
'do something
T2 = Now
MsgBox("It took " & ((T2.Ticks - T1.Ticks) / 10000).ToString & " msecs to sort array")
0

LVL 11

Assisted Solution

you could try "timer()"

This will allow you to get the seconds passed since midnight... But i dont know whats the smalles value for it
0

LVL 11

Expert Comment

Now will only return down to the second :(
0

LVL 2

Assisted Solution

There is the tick, which is 100 nanoseconds
0

LVL 2

Accepted Solution

Looking over the documentation for the DateTime.Now property, it seems that the resolution depends on the System timer.

DateTime.Now: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclassnowtopic.asp

This means that past NT 3.5, you only have 10ms of guaranteed resolution.  An alternative is the Environment.TickCount property, but it claims it is derived from the System Timer as well and additionally its resolution "cannot be less than 500 milliseconds", so DateTime.Now seems best.

Environment.TickCount: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemenvironmentclasstickcounttopic.asp

To measure time as accurately as possible, use the TimeSpan class and its properties.  Here's a simple example that shows the number of milliseconds it takes to perform an operation:

*BEGIN CODE*
Dim startTime as DateTime
Dim endTime as DateTime
Dim ts as TimeSpan

startTime = DateTime.Now
endTime = DateTime.Now

ts = endTime.Subtract(startTime)

MessageBox.Show("Took " & ts.totalMilliseconds & " milliseconds.")
*END CODE*
0

Author Comment

thanx for the info guys,
i got some information from here which can measure in microseconds,
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q172/3/38.asp&NoWebContent=1

and ticks is not gonna help as its again rounds to milliseconds....

and all system.timers rounds to accuracy of 10 ms

any way thanx for all the infor guys,
prashanth
0

## Featured Post

### Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…