Click here to Skip to main content
11,581,982 members (75,450 online)
Click here to Skip to main content

Which code runs faster? The answer may surprise you....

, 25 Feb 2013 CPOL 9.4K 7
Rate this:
Please Sign up or sign in to vote.
This code compares object types instead of the name for the object type.

I saw some code that piqued my interest a few days ago. Basically, this code compared object types instead of the name for the object type.  Being the developer that I am, it set me to thinking about which way is faster.  Here are a couple of routines that do the exact same thing. Which one do you think will run faster? This code is targeted for .NET 4 Client, but should also work in .NET 2 and later.

The StopWatch objects (oStopWatchA and oStopWatchB) are defined as Class level private System.Diagnostics.Stopwatch objects and initialized before the call to the Subs.  The Object object (oObject) is defined as a Class level private System.Object object and initialized before the call to the Subs.  Each routine was run 1,000 times, swapping with each execution to eliminate external factors to the timing.

Private Sub CompareA()
  Dim iLoop As Integer

  oStopWatchA.Start()
  For iLoop = 1 To 1000000
    Select Case oObject.GetType().Name
      Case "Object"
      Case "Exception"
      Case Else
    End Select
  Next
  oStopWatchA.Stop()
End Sub
Private Sub CompareB()
  Dim iLoop As Integer

  oStopWatchB.Start()
  For iLoop = 1 To 1000000
    Select Case oObject.GetType()
      Case GetType(Object)
      Case GetType(Exception)
      Case Else
    End Select
  Next
  oStopWatchB.Stop()
End Sub

The elapsed time for each is listed below (lower numbers are better):

RoutineElapsed Time (seconds)
CompareA32.9136874
CompareB7.7228856

Were you surprised? I was!

Edit: The whole point of this article is to not just do the same thing the same way you always have.  Look at other methods to accomplish your goal.  I was surprised by the speed difference of changing the comparison from a string to a type reference, mainly because I expected the compiler to be able to generate code that pulled the type's name once for the comparison.  I was also expecting the GetType() calls to be executed on every comparison (each Case statement).  

License

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

Share

About the Author

Adam Zuckerman
Software Developer (Senior)
United States United States
Long time software engineer who rambles occasionally about coding, best practices, and other random things.

You may also be interested in...

Comments and Discussions

 
Questionsurprising ^_* Pin
BesSo W Bas20-Mar-13 2:21
memberBesSo W Bas20-Mar-13 2:21 
Questionok Pin
CIDev14-Mar-13 5:17
memberCIDev14-Mar-13 5:17 
GeneralMy vote of 1 Pin
William E. Kempf25-Feb-13 11:02
memberWilliam E. Kempf25-Feb-13 11:02 
GeneralRe: My vote of 1 Pin
Adam Zuckerman25-Feb-13 11:55
memberAdam Zuckerman25-Feb-13 11:55 
And I can safely say that you have no idea what the requirements were for the project I was working on. Comparing types by names has its uses. To dismiss those uses without knowing the requirements, well...

As for comparing Types, you can also get into situations where a Type comparison will match an inherited Type when you may not want that to occur.

Sub Main
  Dim c As PassengerTruck = New PassengerTruck()
  
  Dim b As Boolean = (TypeOf c Is Car)
  Console.WriteLine(b)
  
  Console.WriteLine(c.GetType().Name)
End Sub
 
' Define other methods and classes here
Public Class Car
 
End Class
 
Public Class PassengerTruck
  Inherits Car
  
End Class

The above code returns:
True
PassengerTruck

In my case, the Objects are well defined with appropriate meta data, structural differences, methods, properties, fields, etc. The names of those Objects are unique within the Namespace being occupied.
Long time software engineer who rambles occasionally about coding, best practices, and other random things.
http://justsomevbcode.blogspot.com/

GeneralRe: My vote of 1 Pin
PIEBALDconsult25-Feb-13 12:55
memberPIEBALDconsult25-Feb-13 12:55 
QuestionWRONG Pin
Member 970941720-Feb-13 8:32
memberMember 970941720-Feb-13 8:32 
AnswerRe: WRONG Pin
PIEBALDconsult25-Feb-13 12:31
memberPIEBALDconsult25-Feb-13 12:31 
GeneralMy vote of 3 Pin
AnDev201320-Feb-13 6:52
memberAnDev201320-Feb-13 6:52 
QuestionYou're surprized? Pin
Matt T Heffron19-Feb-13 7:12
memberMatt T Heffron19-Feb-13 7:12 
GeneralMy vote of 4 Pin
not_starman19-Feb-13 5:45
membernot_starman19-Feb-13 5:45 
GeneralMy vote of 2 Pin
Carlos190719-Feb-13 5:40
memberCarlos190719-Feb-13 5:40 
GeneralRe: My vote of 2 Pin
Adam Zuckerman19-Feb-13 15:55
memberAdam Zuckerman19-Feb-13 15:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 25 Feb 2013
Article Copyright 2013 by Adam Zuckerman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid