Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / Visual Basic

Comparative Speed Testing

Rate me:
Please Sign up or sign in to vote.
3.57/5 (9 votes)
28 May 2008CPOL6 min read 29.2K   548   15  
A simple-to-use class for performing comparative, non-benchmarked speed tests when optimising code for execution speed.
#Region "[=== OPTIONS ===]"

Option Strict On
Option Explicit On
Option Compare Binary

#End Region

''' <summary>
''' clsSpeedTestAB_Recursion - Speed test Stack-Push-Loop versus Recursive method calls.
''' </summary>
''' <overview>
''' </overview>
''' <remarks>
''' </remarks>
''' <notes>
''' </notes>
Public Class clsSpeedTestAB_Recursion
  Inherits ZED.Utility.Development.clsBaseSpeedTestAB

#Region "[=== CONSTRUCTORS DESTRUCTORS INITIALISATION ===]"

  ''' <summary>
  ''' Construct a new speed test with prescribed repetitions.
  ''' </summary>
  ''' <param name="aRepetitions"></param>
  ''' <remarks></remarks>
  Public Sub New(ByVal aRepetitions As Int32)
    MyBase.New(aRepetitions)
  End Sub

#End Region

#Region "[=== SPEED TEST COMPONENTS ===]"

  Protected Const kMAX As Int32 = 100

#End Region

#Region "[=== SPEED TEST OVERRIDE METHODS ===]"

  ''' <summary>
  ''' Overriden speed test B setup.
  ''' </summary>
  ''' <remarks></remarks>
  Protected Overrides Sub SetUpTestB()
    Me.f_DescribeA = "Loop, pushing data on a stack."
  End Sub

  ''' <summary>
  ''' Overriden speed test A setup.
  ''' </summary>
  ''' <remarks></remarks>
  Protected Overrides Sub SetUpTestA()
    Me.f_DescribeB = "Loop, calling a function recursively."
  End Sub

  ''' <summary>
  ''' Overridden Speed Test B.
  ''' </summary>
  ''' <remarks></remarks>
  Protected Overrides Sub SpeedTestB()
    Me.TestB(0)
  End Sub

  ''' <summary>
  ''' Overridden Speed Test A.
  ''' </summary>
  ''' <remarks></remarks>
  Protected Overrides Sub SpeedTestA()
    Me.TestA(0)
  End Sub

  Protected Sub TestB(ByVal aNumber As Int32)
    Dim xStack As New Stack
    Dim xDone As Boolean = False
    Dim xDecrease As Boolean = False
    Dim xMax As Int32 = aNumber + kMAX
    Dim xNumber As Int32 = aNumber

    Do While True
      If xDecrease Then
        xNumber = CInt(xStack.Pop)
        If xNumber = aNumber Then Return
      Else
        xStack.Push(xNumber)
        If xNumber = xMax Then
          xDecrease = True
        Else
          xNumber += 1
        End If
      End If
    Loop

  End Sub

  Protected Sub TestA(ByVal aNumber As Int32)
    If aNumber = kMAX Then
      Return
    Else
      Me.TestA(aNumber + 1)
    End If
  End Sub

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
ZED
New Zealand New Zealand
A DEC PDP/11 BasicPlus2 developer from the 80s.

Comments and Discussions