2010-06-08 57 views
0

我想能夠在ASP.net(VBscript)中加載頁面加載時間。將trace =「true」添加到頁面指令是很好的,但我需要實際計時一個事件並將其存儲在一個變量中。ASP.net VB定時器

在ASP中,使用Timer對象很容易,但在.net中我無法在Google上找到任何東西。

我需要沿着線的東西:

Dim startTime 
Dim endTime 

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

乾杯!

回答

1

有是一個DateTime.Now DataTime類的靜態成員,它執行您想要的操作:

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) 
+0

完美的謝謝你,並感謝其他解決方案,我相信他們的工作,以及評分他們 – 2010-06-08 14:02:11

3

嘗試秒錶

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") 

你不必來計算,如果你調用stopWatch.Reset(),您可以重複使用它

拿着從這裏的例子:http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx