What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleCompare the speeds of using class objects, derived class objects, and generic Objects in VB .NET
DescriptionThis example compares the speeds of calling methods from class objects, derived class objects, and generic Objects in VB .NET. It also compares the speeds of overridden and shadowed methods, and interfaces.
Keywordsspeed, performance, class, derived class, interface, Overrides, Shadows
CategoriesTips and Tricks, VB.NET
This program uses the following classes and interfaces.
Public Interface IPerson
    Sub IPersonMethod()
End Interface

Public Class Person
    Public Sub PersonMethod()
    End Sub
    Public Sub SPersonMethod()
    End Sub
    Public Overridable Sub OPersonMethod()
    End Sub
End Class

Public Class Manager
    Inherits Person
    Implements IPerson

    Public Shadows Sub PersonMethod()
    End Sub

    Public Overrides Sub OPersonMethod()
    End Sub

    Public Sub IPersonMethod() Implements _
    End Sub
End Class
When you click various buttons, the program calls the class methods using different variable types. For instance, it calls a Manager object's PersonMethod and a Person object's PersonMethod. The following code shows how it calls the Manager object's overridden version of OPersonMethod.
Private Sub btnOverrides_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    Dim start_time As DateTime
    Dim stop_time As DateTime
    Dim elapsed_time As TimeSpan
    Dim num_trials As Long = Long.Parse(txtNumTrials.Text)
    Dim obj As New Manager
    lblOverrides.Text = ""
    Me.Cursor = Cursors.WaitCursor

    start_time = Now
    For i As Long = 1 To num_trials
    Next i
    stop_time = Now
    elapsed_time = stop_time.Subtract(start_time)
    lblOverrides.Text = _
    Me.Cursor = Cursors.Default
End Sub
Generally the different methods give good performance. The following table describes the methods, shows their times in one test, and shows their decompiled code so you can see the differences.

MethodTimeCompiled Code
000000f7  mov         ecx,dword ptr [ebp-1Ch] 
000000fa  cmp         dword ptr [ecx],ecx 
000000fc  call        dword ptr ds:[007F7170h] 
00000102  nop
000000f7  mov         ecx,dword ptr [ebp-1Ch] 
000000fa  cmp         dword ptr [ecx],ecx 
000000fc  call        dword ptr ds:[007F72B4h] 
00000102  nop
Manager As Person
(Dim obj As Person = New Manager)
000000f7  mov         ecx,dword ptr [ebp-1Ch] 
000000fa  cmp         dword ptr [ecx],ecx 
000000fc  call        F868400B 
00000101  nop
(DirectCast(obj, Manager).PersonMethod)
000000f7  mov         edx,dword ptr [ebp-1Ch] 
000000fa  mov         ecx,7F726Ch 
000000ff  call        7107D95C 
00000104  mov         ecx,eax 
00000106  cmp         dword ptr [ecx],ecx 
00000108  call        F8683F5B 
0000010d  nop
000000f7  mov         ecx,dword ptr [ebp-1Ch] 
000000fa  mov         eax,dword ptr [ecx] 
000000fc  mov         eax,dword ptr [eax+0Ch] 
000000ff  mov         eax,dword ptr [eax+000000F4h] 
00000105  call        dword ptr [eax] 
00000107  nop
000000fc  mov         ecx,dword ptr [ebp-1Ch] 
000000ff  cmp         dword ptr [ecx],ecx 
00000101  call        dword ptr ds:[007F7174h] 
00000107  nop
000000f7  mov         ecx,dword ptr [ebp-1Ch] 
000000fa  mov         eax,dword ptr [ecx] 
000000fc  call        dword ptr [eax+38h] 
000000ff  nop

It's good news that the parent (Person) and derived class (Manager) methods take the same amount of time. Their decompiled code is almost identical so that makes sense.

It's interesting that using a Manager object declared as a Person is faster. The decompile code is slightly simpler. I suspect some compile-time calculations have made the simplification. (Someone who understands this stuff, let me know.)

Casting from a generic Object to a Manager takes longer. That makes sense because there's a whole extra step there.

It also makes some sense that using the IPerson interface adds some overhead, although it seems like the compiler might have been able to avoid that.

It's odd that the Shadows and Overrides methods are faster than the original methods, particularly since the decompiled code looks very similar to the Person and Manager versions. The differences are small but consistent.

The moral is, using a derived class does not impose a performance penalty the way using an interface does in VB 6. Using an interface in VB .NET does impose a penalty but it's not too big (a bit more than 10 percent).
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.