VB.NET  •  Speed Up and Tune Your Code With a Test Class

Listing 1. You can use this class to demonstrate a variety of optimization techniques. For example, the DoubleIt and DoubleIt2 methods perform the same operation. However, only the former method is overridable. A simple benchmark shows that a call to the overridable version runs about 35 percent slower than a call to the nonoverridable (or sealed) one. Other optimization tests provided here include a method to put the garbage collector under pressure.

Class TestObject
   Implements ITestInterface
   Implements IDisposable
   ' an overridable function
   Overridable Function DoubleIt(ByVal n As Integer) _
      As Integer
      Return n * 2
   End Function
   ' a non-overridable function
   Function DoubleIt2(ByVal n As Integer) As Integer
      Return n * 2
   End Function
   ' an interface method
   Sub TestMethod() Implements _
      ' do nothing
   End Sub
   ' a method that returns an integer
   Function GetInteger(ByVal n As Integer) As Integer
      Return n
   End Function
   ' a method that boxes an int and returns an object 
   Function GetObject(ByVal n As Integer) As Object
      Return n
   End Function

   Declare Function OpenClipboard Lib "user32" _
      (ByVal hwnd As Integer) As Integer
   Declare Function CloseClipboard Lib "user32" _
      () As Integer
   ' put the garbage collector under pressure
   Dim dummyArr(1000) As Byte
   Sub New()
      ' open the clipboard, associate with this task
   End Sub

   Public Sub Dispose() Implements IDisposable.Dispose
   End Sub
   Protected Overrides Sub Finalize()
   End Sub
   Event TestEvent()
End Class

Interface ITestInterface
   Sub TestMethod()
End Interface