Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake classes faster using Implements
KeywordsImplements, classes, interface
CategoriesSoftware Engineering, Tips and Tricks
 
This program uses three objects. It invokes the Value property let procedure for each object many times so you can see which is fastest.

The first object is declared to be of type Object and it is set to a new SpecificClass object that provides the property procedure directly.

The second object is a declared to be of type ImplementorClass. This class uses the Implements keyword to indicate that it implements the Value property procedures defined by the ImplementedClass class.

The third object is a declared to be of type SpecificClass. This class provides the property procedure directly.

You will find that the generic Object takes 10 to 12 times as long as the others.

Of course using specific objects is also fast. The point is that using Implements you can manage several different classes that all implement the same features quickly without using generic Objects. My book Ready-to-Run Visual Basic Algorithms has a chapter on object-oriented algorithms in Visual Basic that covers this and some other interesting stuff.

 
Private Sub CmdGo_Click()
Dim i As Long
Dim max As Long
Dim start_time As Single
Dim generic_time As Single
Dim implements_time As Single
Dim specific_time As Single
Dim generic_object As Object
Dim specific_object As New SpecificClass
Dim implements_object As New ImplementorClass

    Set generic_object = New SpecificClass
    GenericLabel.Caption = ""
    ImplementsLabel.Caption = ""
    SpecificLabel.Caption = ""
    MousePointer = vbHourglass
    DoEvents

    ' Generic.
    max = CLng(IterText.Text)
    start_time = Timer
    For i = 1 To max
        generic_object.Value = 12345
    Next i
    generic_time = Timer - start_time
    GenericLabel.Caption = Format$(generic_time, "0.00")

    ' Implements.
    start_time = Timer
    For i = 1 To max
        implements_object.Value = 12345
    Next i
    implements_time = Timer - start_time
    ImplementsLabel.Caption = _
        Format$(implements_time, "0.00") & _
        " (" & _
        Format$(generic_time / implements_time, "0.0") & _
        " times as fast)"
    
    ' Specific.
    start_time = Timer
    For i = 1 To max
        specific_object.Value = 12345
    Next i
    specific_time = Timer - start_time
    SpecificLabel.Caption = _
        Format$(specific_time, "0.00") & _
        " (" & _
        Format$(generic_time / specific_time, "0.0") & _
        " times as fast)"
    
    MousePointer = vbDefault
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated