Meddelande

Minska
No announcement yet.

.Net-Class för att testa hastigheter på funktioner/subbar

Minska
X
  • Filter
  • Klockan
  • Show
Clear All
new posts

  • .Net-Class för att testa hastigheter på funktioner/subbar

    Kod:
        Sub page_load(ByVal s As Object, ByVal ev As EventArgs) Handles Me.Load
            Dim st As SpeedTester
            st = New SpeedTester(Me, "ListLen1", 1, New Object() {"Detta, är, ett, hastighetstest,, som, jag, nu,, utför,,,", ","})
            results.Text = st.ToString()
            st = New SpeedTester(Me, "ListLen2", 1, New Object() {"Detta, är, ett, hastighetstest,, som, jag, nu,, utför,,,", ","})
            results.Text += "<br />" & st.ToString
            st = New SpeedTester(Me, "ListLen3", 1, New Object() {"Detta, är, ett, hastighetstest,, som, jag, nu,, utför,,,", ","})
            results.Text += "<br />" & st.ToString
        End Sub
    
    'Räknar förekomsten av kommatecken med en replace
        Public Function ListLen1(ByVal inStr As String, ByVal chr As Char) As Int16
            Return CLng(inStr.Length - inStr.Replace(chr, String.Empty).Length)
        End Function
    
    'Räknar förekomsten av kommatecken med split
        Public Function ListLen2(ByVal inStr As String, ByVal chr As Char) As Int16
            Return CLng(inStr.Split(chr).Length - 1)
        End Function
    
    'Räknar förekomsten av kommatecken med regexp
        Public Function ListLen3(ByVal inStr As String, ByVal chr As Char) As Int16
            Return New Regex(chr, RegexOptions.Compiled).Matches(inStr).Count
        End Function

    Kod:
    Imports Microsoft.VisualBasic.CallType
    Imports System
    Imports System.Runtime.InteropServices
    Imports System.ComponentModel
    Imports System.Threading
    
    Public Class SpeedTester
            Private _FunctionToTest As String
            Private _TimesToRun As Integer
            Private _Args() As Object
            Private _ObjectRef As Object
    
            Sub New(ByVal ObjectRef As Object, ByVal FunctionToTest As String, ByVal TimesToRun As Integer, ByVal Args() As Object)
                Me._ObjectRef = ObjectRef
                Me._FunctionToTest = FunctionToTest
                Me._TimesToRun = TimesToRun
                Me._Args = Args
            End Sub
    
            Public Overrides Function ToString() As String
                Dim list As New Generic.List(Of Double)
                For i As Integer = 0 To _TimesToRun
                    Dim hpt As New HiPerfTimer
                    hpt.Start()
                    For x As Int32 = 0 To 1000
                        CallByName(_ObjectRef, _FunctionToTest, vbMethod, _Args)
                    Next x
                    hpt.Stop()
                    list.Add(hpt.Duration)
                Next i
    
                Dim sum As Double = 0.0
                For Each Item As Double In list
                    sum += Item
                Next
                Return String.Format("{0}: {1}", _FunctionToTest, sum.ToString)
            End Function
    
            Public Class HiPerfTimer
    
                <DllImport("Kernel32.dll")> _
                Private Shared Function QueryPerformanceCounter(ByRef lpPerformanceCount As Long) As Boolean
                End Function
    
                <DllImport("Kernel32.dll")> _
                Private Shared Function QueryPerformanceFrequency(ByRef lpFrequency As Long) As Boolean
                End Function
    
                Private startTime As Long
                Private stopTime As Long
                Private freq As Long
    
                Public Sub New()
                    startTime = 0
                    stopTime = 0
                    If QueryPerformanceFrequency(freq) = False Then
                        Throw New Win32Exception
                    End If
                End Sub
    
                Public Sub Start()
                    Thread.Sleep(0)
                    QueryPerformanceCounter(startTime)
                End Sub
    
                Public Sub [Stop]()
                    QueryPerformanceCounter(stopTime)
                End Sub
    
                Public ReadOnly Property Duration() As Double
                    Get
                        Return CType((stopTime - startTime), Double) / CType(freq, Double)
                    End Get
                End Property
            End Class
        End Class
    Qui rogat, non errat
Working...
X