Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

A general benchmarking class

Total Hit ( 2983)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Many times we need to benchmark some routine or a block of code. This class will help you to benchmark a routine or a block of code.

Click here to copy the following block
Public Class BenchMark
  Delegate Sub BenchmarkDelegate()
  Enum BenchmarkModes
    DontShow
    Console
    MessageBox
  End Enum

  Public StartTime As Date
  Public EndTime As Date
  Public Elapsed As TimeSpan
  Public Sub StartBenchMark()
    StartTime = Now
  End Sub
  Public Sub EndBenchMark(Optional ByVal ShowMsg As Boolean = True)
    EndTime = Now
    Elapsed = EndTime.Subtract(StartTime)
    If ShowMsg = True Then
      ShowMessage()
    End If
  End Sub
  Public Sub ShowMessage()
    Dim msg As String
    msg = "Start Time :" & StartTime.ToString & vbCrLf
    msg &= "End Time :" & EndTime.ToString & vbCrLf
    msg &= "Difference (hh:mm:ss.ms) :" & Elapsed.ToString & vbCrLf
    MsgBox(msg)
  End Sub
  Public Function BenchmarkRoutine(ByVal routine As BenchmarkDelegate, _
      Optional ByVal mode As BenchmarkModes = BenchmarkModes.MessageBox) As TimeSpan

    StartTime = Now
    routine.Invoke() ' run the procedure to be benchmarked
    EndTime = Now
    Elapsed = EndTime.Subtract(StartTime)
    If mode = BenchmarkModes.Console Then
      Debug.WriteLine(Elapsed.ToString)
    ElseIf mode = BenchmarkModes.MessageBox Then
      ShowMessage()
    End If

    ' return result to caller
    Return Elapsed
  End Function
End Class

How to use this class to benchmark a code block

Click here to copy the following block
Sub BenchmarkBlockTest()
  Dim bm As New BenchMark

  Dim c As System.Collections.Hashtable
  Dim i As Integer
  bm.StartBenchMark() '//Start

  For i = 0 To 1000000
    c.Add("Item" & i, "Key" & i) '//Add Key-value Pair
  Next

  bm.EndBenchMark() '//End
End Sub

How to use this class to benchmark a routine

Click here to copy the following block
Sub BenchmarkRoutineTest()
  Dim bm As New BenchMark
  bm.BenchmarkRoutine(AddressOf FillVBNetCol, BenchMark.BenchmarkModes.MessageBox)
End Sub

'//9 sec
Sub FillVBNetCol()
  Dim vbnetCol As System.Collections.Hashtable
  Dim i As Integer
  For i = 0 To 1000000
    vbnetCol.Add("Item" & i, "Key" & i) '//Add Key-value Pair
  Next
End Sub



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.