 |
|
 |
Which one is the better way to compare two strings?
Is it ==
Is it Equals
Is it Compare
Is it CompareTo
please specify ..
|
|
|
|
 |
|
 |
In your loop, you seem to be comparing the textbox.text values. I would suggest reading them into string variables beforehand then comparing the string.
|
|
|
|
 |
|
 |
The str.Equals comparison, as well as the == operator, compare memory locations, not the actual content of the strings. Personally, I have always used the str.CompareTo method, an implementation of the IComparable interface. Another option is the static String.CompareOrdinal. After modifying your program to use these, I found what I had assumed - there is virtually no difference in processing time for .Compare, .CompareTo, .CompareOrdinal, and the hash code comparison. Running the same method multiple times shows a variation of up to 16 milliseconds between each run of 10000 comparisons on my 2.8 GHz p4. I had always wondered if there was any difference between them, but was too lazy to write a program like this. Thanks!
|
|
|
|
 |
|
 |
Really? String's Equals and == work even if the strings are not interned. If they did memory reference comparison, then comparison between non-interned strings would fail, wouldn't it?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
 |
|
 |
iammudman wrote:
The str.Equals comparison, as well as the == operator, compare memory locations, not the actual content of the strings.
C# != Java.
Try this code in your IDE and tell me what it does:
if ("test0" == "test" + "0")
Console.WriteLine("equal");
else
Console.WriteLine("not equal");
I see dead pixels
Yes, even I am blogging now!
|
|
|
|
 |
|
 |
It says they are equal, what did i miss?
I have added compareto and compareordinal to the tool, might help checking their timings as well
Also have added this message box there, it says its equal i.e.
if ("test0" == "test" + "0")
Console.WriteLine("equal");
else
Console.WriteLine("not equal");
says its equal, it was always supposed to say its equal, wasnt it?
|
|
|
|
 |
|
 |
Exactly my point. I was trying to prove that the "==" operator does not compare pointers, it actually compares the string contents.
I see dead pixels
Yes, even I am blogging now!
|
|
|
|
 |
|
|
 |
|
 |
String class objects - reference type only - correction in previous msg - sorry for the error
|
|
|
|
 |
|
 |
This is great! Tried the same for this program, observed the mentioned results. But what does it mean now? what is the best way to compare strings? FxCop says use String.Compare passing the current culture info. What do you say about that?
and comparing memory locations would be good if i had to check if those encapsulated strings 'objects' are the same, right? what if i had to check the strings in different string 'objects' are same or not?
|
|
|
|
 |
|
 |
First off, use String.Compare unless you don't need globalization.
What are you trying to do? Try Boyer Moore search[^] if you're trying to search strings. If you want to compare exact results, use which ever of those methods you choose, preferrably String.Compare. Maybe you can consider using pointers if you REALLY need to...
There isn't really much more optimization you can do without looking at the bigger picture.
|
|
|
|
 |
|
 |
This is true for most types but String.Equals() and the == operator are overriden by the framework. So comparisons with these don't test refrence quality like a normal object, they test content equality (the actual value of the contents of the object). Just thought I should clear it up in case any doubt remained.
|
|
|
|
 |
|
 |
1500msec to 31msec... is there something I'm not catching?
|
|
|
|
 |
|
 |
Nope, thats right, try escaping the checks, i couldnt believe that a simple for loop could also take so much time to execute.
also i tried populating a hash table with those many records and as expected retrieving a particular record from hash is faster. help me improve this, thanks
|
|
|
|
 |