I’ve been working with some programs here at work for about a month now that have a lot of string parsing and such going on. I’ve been advised to use a char array for this stuff as opposed to a string because the char array is faster. I understand why a char array is fast, but what is it about the string type that makes it slower? What data structure is it implementing and is there any way to make it as fast as a char array?
Share
The most obvious difference is that
stringis immutable. So you can’t modify parts of it and need to create a completely new copy on each modification.String itself has a very special implementation (it’s a variable size class) and is not backed by an array. I see no reason why read-only access to
chars in a string should be slow.So if you want to change small parts of a string, you need to use either
StringBuilderorchar[]. Of these twochar[]is/was faster sinceStringBuilderhas additional verifications and indirections. But since this is an implementation detail it might have changed since I last tested it.Just benchmarked it, and as of .NET 4 setting a member of
char[]is about four times as fast compared to aStringBuilder. But both can do more than 200 milion assignments per second, so it rarely matters in practice.Reading from a
char[]is slightly faster (25% for my test code) that reading fromstring. Reading fromStringBuilderon the other hand is slower (a factor of 3) than reading fromchar[].In all benchmarks I neglected the overhead of my other code. This means that my test underestimates the differences a bit.
My conclusion is that while
char[]is faster than the alternatives it only matters if you’re going over hundreds of megabytes per second.(Yes, I know my benchmark code isn’t very good, but I don’t think it makes a big difference.)