The ‘=’ operator vs. Equals() in VB.Net

As I was reading yet another chapter in Code Complete Second Edition by Steve McConnell I started to think about the VB operator '=' versus the C equivalent '=='. Even though im a big VB fan I must say the C style is much better. In VB we use the same operator for two purposes, in one place we use it for ASSIGNING data and in another we use it for COMPARING data.

However, I thought that why not use Equals(obj,1 obj2) instead of '=' for comparisons, if not only for the sake of maintaining a more consequent code. Further down that lane I started to wonder if there might be any pros or cons in regard to performance with the two. I have always that that Equals(obj1, obj2) just wraps a 'obj1 = obj2' statement. Daaah NOT ;-).

Here is my test case:

Dim startTime As DateTime
Dim endTime As DateTime

Dim strTestSubject1 As String = ".net is gooood...
Dim strTestSubject2 As String = ".net is baaaaad..."

Dim i As Integer = 0

startTime = DateTime.Now

While i < 99999
If strTestSubject1 = strTestSubject2 Then
'If strTestSubject1.Equals(strTestSubject2)
'Do nothing
End If
Console.WriteLine(i)
i = i + 1
End While
endTime = DateTime.Now
Console.WriteLine(vbCrLf & endTime.Subtract(startTime).TotalSeconds)

The results were not STUNNING but there is actually a difference:

Using the = operator

17,1143025 sec.

Using the Equals() function

15,473205 sec.


The difference is about 1,6410975 seconds ( a rough estimate by just glancing the numbers ;-) ). It does not
seem like a major difference but using the strTestSubject1.Equals(strTestSubject2) is still 0.9 % faster.

There seems to be a lot of discussion about this:
Nick Schweitzer posts a comparison on the both claiming that '=' uses old VB6 libraries…
Cory Smith rebuttals it in his post saying it does not…

Whatever the reason is, it sure seems like Equals IS faster.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Monday, March 26, 2007 8:07 AM
Print

Comments on this entry:

# re: The ‘=’ operator vs. Equals() in VB.Net

Left by Brian at 12/19/2008 10:36 PM
Gravatar
"=" tests reference equality. If the two reference the same object, they are equal according to "=". The "equals()" method is an overridable Object method like "toString()" that allows you to test your objects equality in a way that says "do their representative values equal each other?"

Java, c# and the like also implement the equals() method, since ==, like vb, tests reference equality.

# re: The ‘=’ operator vs. Equals() in VB.Net

Left by erx at 5/6/2010 12:37 PM
Gravatar
thank you Brian - for pointing out the question that every person coming here from google had, that the original poster did not point out!

# re: The ‘=’ operator vs. Equals() in VB.Net

Left by kane at 9/21/2010 7:58 AM
Gravatar
thanks for the post, i was just pondering the same idea a few minutes ago before i found your response. cheers!

# re: The ‘=’ operator vs. Equals() in VB.Net

Left by Ivan Ferrer at 10/28/2010 7:16 PM
Gravatar
The one I never hear about is 'Is'.
strTestSubject1 Is strTestSubject2
Is the fastest one to know if both objects point to the same instance.
Using 99999999 iterations it threw the Best results:
=: 2,865
Equals: 1,00225
Is: 0,322 <-----!!

# re: The ‘=’ operator vs. Equals() in VB.Net

Left by sharie at 2/8/2011 5:03 AM
Gravatar
what is the code for the equal sign??? can you please answer???

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910