Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB .NET Timer

Status
Not open for further replies.

jgobrien

Programmer
Apr 15, 2005
27
0
0
US
In VB6 you had the ability to time how long something took to execute:

Dim dStart As Double
dStart = Timer

Dim c As String
Dim i As Long
For i = 0 To 1000000
c = "hi"
Next i

Dim dEnd As Double
dEnd = Timer

MsgBox "Process took: " & Round(((dEnd - dStart) * 1000), 0) & " milliseconds."



I'm looking for the VB .NET equivalent of this.
I also need millisecond resolution (or better).

Anyone know how to do this?
 
Imports VB = Microsoft.VisualBasic


Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
Dim dStart As Double
dStart = VB.Timer()

Dim c As String
For i As Int64 = 0 To 1000000000
c = "hi"
Next i

Dim dEnd As Double
dEnd = VB.Timer()

MsgBox("Process took: " & System.Math.Round((dEnd - dStart) * 1000, 0) & " milliseconds.")

End Sub
 
Be careful of the Heisen effect. By performing a timing of the process on the same machine you will change the amount of time it takes to run. It would be more accurate to log the start time and end time. If your process is < 1 second long however, you may need to run it a few times then find the average run time.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Thanks, I didn't know that would work.


I actually ended up doing this (But the timer way also works):

---------------

Dim iStart As Long = Now.Ticks
'Stuff
Dim iEnd As Long = Now.Ticks

Milliseconds = Math.Round(((iEnd - iStart) / 10000), 0)

----------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top