views:

40

answers:

3

I would like to be able to time a page load time in ASP.net (VBscript). Adding Trace="true" to the page directive is nice, but I need to actually time an event and store it in a variable.

In ASP it was easy with the Timer object, but in .net I can't find anything on Google.

I need something along the lines of:

Dim startTime
Dim endTime

startTime = now()
doBigFunction()
endTime = now()
response.write("That took " & endTime - startTime & " milliseconds")

Cheers!

+1  A: 

For this level you can use the DateTime.Now property. A TimeSpan is the difference between two DateTime values.

For something more accurate you need the Stopwatch.

ChrisF
+3  A: 

Try a StopWatch

    Dim stopWatch As New Stopwatch()
    stopWatch.Start()
    Thread.Sleep(10000)
    stopWatch.Stop()
    ' Get the elapsed time as a TimeSpan value.
    Dim ts As TimeSpan = stopWatch.Elapsed

    ' Format and display the TimeSpan value.
    Dim elapsedTime As String = _ 
         String.Format("{0:00}:{1:00}:{2:00}.{3:00}", _
         ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
    Console.WriteLine(elapsedTime, "RunTime")

You don't have to calculate and you can reuse it if you call stopWatch.Reset()

Took the example from here: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

SchlaWiener
+1  A: 

There is a DateTime.Now static member of the DataTime class that does what you want it to do:

    Dim startTime As DateTime
    Dim endTime As DateTime

    startTime = DateTime.Now
    For i = 0 To 100000000
        Dim j As Integer = i * 2
    Next
    endTime = DateTime.Now
    Dim result As TimeSpan = endTime - startTime
    Console.WriteLine(result.Milliseconds)
SWeko
Perfect thank you, and thanks to the other solutions as well, I'm sure they work as well and will rate them up
Tom Gullen