Performance Count

摘要:Performance Count

在撰寫程式的過程中,有些時候我們要知道某些程式碼的效能,那麼要怎麼做呢?我們可以簡單的利用Win32 API來做這樣的功能
 


Imports System.Runtime.InteropServices

Public Class cQueryPerformance

    Private _Freq As Int64
    Private _Count1, _Count2 As Int64

     _
Public Shared Function QueryPerformanceFrequency( _
    ByRef lpFrequency As Int64) As Integer
    End Function

     _
    Public Shared Function QueryPerformanceCounter( _
        ByRef lpPerformanceCount As Int64) As Integer
    End Function

    ''' 
    ''' 設定起始點
    ''' 
    ''' 
    ''' 
    Public Function StartCount() As Boolean
        If QueryPerformanceFrequency(_Freq) <> 0 Then
            If QueryPerformanceCounter(_Count1) <> 0 Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function

    ''' 
    ''' 設定結束點成功並傳回經過時間(ms)
    ''' 
    ''' 
    ''' 
    Public Function EndCount() As Int64
        If QueryPerformanceCounter(_Count2) <> 0 Then
            Return (_Count2 - _Count1) * 1000 / _Freq
        Else
            Return -1
        End If
    End Function
End Class