views:

3007

answers:

8

Which is faster? This:

bool isEqual = (MyObject1 is MyObject2)

Or this:

bool isEqual = ("blah" == "blah1")

It would be helpful to figure out which one is faster. Obviously, if you apply .ToUpper() to each side of the string comparison like programmers often do, that would require reallocating memory which affects performance. But how about if .ToUpper() is out of the equation like in the above sample?

+1  A: 

How about you tell me? :)

Take the code from this Coding Horror post, and insert your code to test in place of his algorithm.

HanClinto
+3  A: 

The first one is used to compare types not values. If you want to compare strings with a non-sensitive case you can use:


     string toto = "toto";
     string tata = "tata";
     bool isEqual = string.Compare(toto, tata, StringComparison.InvariantCultureIgnoreCase) == 0; 
     Console.WriteLine(isEqual);  
labilbe
+3  A: 

According to the book Maximizing .NET Performance the call

bool isEqual = String.Equals("test", "test");

is identical in performance to

bool isEqual = ("test" == "test");

The call

bool isEqual = "test".Equals("test");

is theoretically slower than the call to the static String.Equals method, but I think you'll need to compare several million strings in order to actually detect a speed difference.

My tip to you is this; don't worry about which string comparison method is slower or faster. In a normal application you'll never ever notice the difference. You should use the way which you think is most readable.

Frode Lillerud
A: 

Comparing strings with a "==" operator compares the contents of the string vs. the string object reference. Comparing objects will call the "Equals" method of the object to determine whether they are equal or not. The default implementation of Equals is to do a reference comparison, returning True if both object references are the same physical object. This will likely be faster than the string comparison, but is dependent on the type of object being compared.

JoshL
+4  A: 

I'm a little confused here.

As other answers have noted, you're comparing apples and oranges. ::rimshot::

If you want to determine if an object is of a certain type use the is operator.

If you want to compare strings use the == operator (or other appropriate comparison method if you need something fancy like case-insensitive comparisons).

How fast one operation is compared to the other (no pun intended) doesn't seem to really matter.


After closer reading, I think that you want to compare the speed of string comparisions with the speed of reference comparisons (the type of comparison used in the System.Object base type).

If that's the case, then the answer is that reference comparisons will never be slower than any other string comparison. Reference comparison in .NET is pretty much analogous to comparing pointers in C - about as fast as you can get.

However, how would you feel if a string variable s had the value "I'm a string", but the following comparison failed:

if (((object) s) == ((object) "I'm a string")) { ... }

If you simply compared references, that might happen depending on how the value of s was created. If it ended up not being interned, it would not have the same reference as the literal string, so the comparison would fail. So you might have a faster comparison that didn't always work. That seems to be a bad optimization.

Michael Burr
The other answers have missed the key point, which is that the "is" operator doesn't do anything like what the questioner thinks it does.
Robert Rossney
A: 

I'd assume that comparing the objects in your first example is going to be about as fast as it gets since its simply checking if both objects point to the same address in memory.

As it has been mentioned several times already, it is possible to compare addresses on strings as well, but this won't necessarily work if the two strings are allocated from different sources.

Lastly, its usually good form to try and compare objects based on type whenever possible. Its typically the most concrete method of identification. If your objects need to be represented by something other than their address in memory, its possible to use other attributes as identifiers.

Soviut
You missed the meaning of the "is" operator as well, I think?
Benjamin Podszun
A: 

I have a file name like ABC_KK_123456_2008081218.txt

here KK is the branch code and 12345 is the employee#

I would like to parse the file name by branch code and employee#.

How can I use stringcomparisontype here?

Any help will be great!

tx

Dave
Don't add unrelated questions as answers to other questions. Start a new one.
Benjamin Podszun
A: 

If I understand the question and you really want to compare reference equality with the plain old "compare the contents": Build a testcase and call object.ReferenceEquals compared against a == b.

Note: You have to understand what the difference is and that you probably cannot use a reference comparison in most scenarios. If you are sure that this is what you want it might be a tiny bit faster. You have to try it yourself and evaluate if this is worth the trouble at all..

Benjamin Podszun