|
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 _
ITestInterface.TestMethod
' 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
OpenClipboard(0)
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
CloseClipboard()
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
CloseClipboard()
MyBase.Finalize()
End Sub
Event TestEvent()
End Class
Interface ITestInterface
Sub TestMethod()
End Interface
|