VB.NET程序运行耗时精确计量方法之一

前端之家收集整理的这篇文章主要介绍了VB.NET程序运行耗时精确计量方法之一前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Imports System.Threading
'精确计量程序运行时间的STOPWATCH
Module Module1
    Sub Main1()
        ' Create new Stopwatch instance.
        Dim watch As New Diagnostics.Stopwatch
        watch.Start()     '启动

        ' Measure.
        For i As Integer = 0 To 1000 - 1
            Threading.Thread.Sleep(1)
        Next

        ' 停止并显示时间
        watch.Stop()
        Console.WriteLine(watch.Elapsed.TotalMilliseconds)

        ' This isn't measured.
        For i As Integer = 0 To 1000 - 1
            Threading.Thread.Sleep(1)
        Next

        ' Begin measuring again.
        watch.Start()

        ' Measure.
        For i As Integer = 0 To 1000 - 1
            Threading.Thread.Sleep(1)
        Next

        ' Stop measuring again (not always needed).
        watch.Stop()
        Console.WriteLine(watch.Elapsed.TotalMilliseconds)

        Console.ReadLine()
    End Sub


    Sub Main2()
        ' Create a Stopwatch and sleep for zero milliseconds.
        Dim stopwatch As New Diagnostics.Stopwatch   ' = stopwatch.StartNew
        stopwatch.Start()

        Thread.Sleep(0)
        stopwatch.Stop()

        ' Write the current time.
        Console.WriteLine(stopwatch.ElapsedMilliseconds)
        Console.WriteLine(DateTime.Now.ToLongTimeString)

        ' Start a new Stopwatch.
        stopwatch = stopwatch.StartNew
        Thread.Sleep(5000)
        stopwatch.Stop()
        Console.WriteLine(stopwatch.ElapsedMilliseconds)
        Console.WriteLine(DateTime.Now.ToLongTimeString)

        ' Start a new Stopwatch.
        stopwatch = stopwatch.StartNew
        Thread.Sleep(1000)
        stopwatch.Stop()
        Console.WriteLine(stopwatch.ElapsedMilliseconds)

        ' Start a new Stopwatch and use SpinWait.
        stopwatch = stopwatch.StartNew
        Thread.SpinWait(1000000000)
        stopwatch.Stop()
        Console.WriteLine(stopwatch.ElapsedMilliseconds)

        Console.ReadLine()
    End Sub


    Sub Main()
        'Dim sw As New Diagnostics.Stopwatch
        'sw.Start()

        'For i As Integer = 0 To 100000
        '    For k As Integer = 0 To 100000

        '    Next
        'Next
        'sw.Stop()
        'Console.WriteLine(sw.ElapsedMilliseconds)
        'Console.ReadLine()


    End Sub

End Module

猜你在找的VB相关文章