Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison performance in C#

Tags:

There are a number of ways to compare strings. Are there performance gains by doing one way over another?

I've always opted to compare strings like so:

string name = "Bob Wazowski"; if (name.CompareTo("Jill Yearsley") == 0) {     // whatever... } 

But I find few people doing this, and if anything, I see more people just doing a straight == comparison, which to my knowledge is the worst way to compare strings. Am I wrong?

Also, does it make a difference in how one compares strings within LINQ queries? For example, I like to do the following:

var results = from names in ctx.Names               where names.FirstName.CompareTo("Bob Wazowski") == 0               select names; 

But again, I see few people doing string comparisons like so in their LINQ queries.

like image 920
Jagd Avatar asked May 13 '09 16:05

Jagd


People also ask

Are string comparisons slow?

Comparing two strings is very slow and expensive. Most algorithms require iterating through entire string and matching each character.

Is string comparison faster than integer comparison?

Instead of comparing content, we can compare their memory addresses which are just integers. So, comparing integers are far better than comparing two long strings character by character. This is much faster and less CPU utilization than comparing character by character.

What is the time complexity of string comparison?

String comparisons typically do a linear scan of the characters, returning false at the first index where characters do not match. The time complexity is O(N) and the actual time taken depends on how many characters need to be scanned before differences statistically emerge.

Can I use == to compare strings in C?

You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.


2 Answers

According to Reflector

"Hello" == "World" 

is the same as

String.Equals("Hello", "World"); 

which basically determines if they are the same reference object, if either of them is null, which would be an automatic false if one was null and the other was not, and then compares each character in an unsafe loop. So it doesn't care about cultural rules at all, which usually isn't a big deal.

and

"Hello".CompareTo("World") == 0 

is the same as

CultureInfo.CurrentCulture.CompareInfo.Compare("Hello", "World", CompareOptions.None); 

This is basically the opposite as far as functionality. It takes into consideration culture, encoding, and everything else with the string in to context.

So I would imagine that String.CompareTo is a couple of orders of magnitude slower than the equality operator.

as for your LINQ it doesn't matter if you are using LINQ-to-SQL because both will generate the same SQL

var results = from names in ctx.Names           where names.FirstName.CompareTo("Bob Wazowski") == 0           select names; 

of

SELECT [name fields] FROM [Names] AS [t0] WHERE [t0].FirstName = @p0 

so you really aren't gaining anything for LINQ-to-SQL except harder to read code and probably more parsing of the expressions. If you are just using LINQ for standard array stuff then the rules I laid out above apply.

like image 199
Nick Berardi Avatar answered Sep 20 '22 15:09

Nick Berardi


In my opinion, you should always use the clearest way, which is using ==!

This can be understood directly: When "Hello" equals "World" then do something.

if ("Hello" == "World")     // ... 

Internally, String::Equals is invoked which exists explicitly for this purpose - Comparing two strings for equality. (This has nothing to do with pointers and references etc.)

This here isn't immediately clear - Why compare to zero?

if ("Hello".CompareTo("World") == 0) 

.CompareTo isn't designed just for checking equality (you have == for this) - It compares two strings. You use .CompareTo in sorts to determine wheter one string is "greater" than another. You can check for equality because it yield zero for equal strings, but that's not what it's concepted for.

Hence there are different methods and interfaces for checking equality (IEquatable, operator ==) and comparing (IComparable)

Linq doesn't behave different than regular C# here.

like image 36
Dario Avatar answered Sep 21 '22 15:09

Dario