Click here to Skip to main content
Full site     10M members (39.4K online)    

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

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).  

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
Questionsurprising ^_*
BesSo W Bas
20 Mar '13 - 2:21 
Questionok
CIDev
14 Mar '13 - 5:17 
GeneralMy vote of 1
William E. Kempf
25 Feb '13 - 11:02 
GeneralRe: My vote of 1
Adam Zuckerman
25 Feb '13 - 11:55 
GeneralRe: My vote of 1
PIEBALDconsult
25 Feb '13 - 12:55 
QuestionWRONG
Member 9709417
20 Feb '13 - 8:32 
AnswerRe: WRONG
PIEBALDconsult
25 Feb '13 - 12:31 
GeneralMy vote of 3
AnDev2013
20 Feb '13 - 6:52 
QuestionYou're surprized?
Matt T Heffron
19 Feb '13 - 7:12 
GeneralMy vote of 4
not_starman
19 Feb '13 - 5:45 
GeneralMy vote of 2
Carlos1907
19 Feb '13 - 5:40 
GeneralRe: My vote of 2
Adam Zuckerman
19 Feb '13 - 15:55 

Last Updated 25 Feb 2013 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013