Faster string comparison in C #

I have a program that compares two files. I did a visual analysis of the studio and found my comparison time was long. Is there a faster way to compare two strings than this? (I can't use a parallel foreach because it might cause errors.) I'm currently using a parallel dictionary, but I'm open to other options. :)

var metapath = new ConcurrentDictionary<string, string>();
foreach(var me in metapath)
 if (line.StartsWith(me.Key.ToString()))



source to share

3 answers

First of all, clear ToString()

with me.Key.ToString()


Then use an ordinal string comparison (assuming it doesn't affect correctness):

line.StartsWith(me.Key, StringComparison.Ordinal);


This is useful because standard string collations follow different Unicode rules for what is equal. For example, normalized and denormalized sequences should be treated as equal. Ordinal

simply compares the raw character data, ignoring Unicode equality rules. It is described in more detail here , or here (which asserts it faster, but without any numbers).

Last, the code profile. You might be surprised, but most of the time the slow part is not what you think it is. For example, this might be the part where you add things to the dictionary.



If you are comparing strings accurately, String.Equals is not bad:

String.Equals(line, me.Key)


Have you seen this: What is the fastest (inline) comparison for string types in C #



It is not clear what you mean by "comparison", but if you do not mean "sort", i.e. want to check for plagiarism or something like that, how about hashing strings and comparing hash?

It depends on the size of your dataset as to whether there is any benefit. Big and small are very subjective terms.



All Articles