.NET type string against char array

I work with some programs here when I work for about a month and I have a lot of parsing of strings. I am advised to use a char array for this stuff, not a string because the char array is faster. I understand why the char array is fast, but what is the type of string that makes it slower? What data structure is implemented and is there a way to do this as fast as a char array?

+6
source share
2 answers

The most obvious difference is that string is immutable. Therefore, you cannot change parts of it and create every new modification of a completely new copy.

The string itself has a very special implementation (this is a class of variable dimension) and is not supported by the array. I see no reason why access to read-only for char in a string should be slow.

So, if you want to change the small parts of a string, you need to use either StringBuilder or char[] . Of these two char[] there is / faster, since StringBuilder has additional checks and directions. But since this is an implementation detail, it may have changed since the last time I tested it.


Just compare it, and with .NET 4, setting the char[] member is about four times faster than StringBuilder . But both can perform more than 200 millionth tasks per second, so this rarely matters in practice.

Reading from char[] slightly faster (25% for my test code), which reads from string . Reading with StringBuilder on the other hand, is slower (3 times) than reading from char[] .

In all tests, I neglected the overhead of my other code. This means that my test underestimates the differences a bit.

My conclusion is that while char[] faster than alternatives, it only matters if you go through hundreds of megabytes per second.


 //Write StringBuilder StringBuilder sb = new StringBuilder(); sb.Length = 256; for(int i=0; i<1000000000; i++) { int j = i&255; sb[j] = 'A'; } //Write char[] char[] cs = new char[256]; for(int i=0; i<1000000000; i++) { int j = i&255; cs[j] = 'A'; } // Read string string s = new String('A',256); int sum = 0; for(int i=0; i<1000000000; i++) { int j = i&255; sum += s[j]; } //Read char[] char[] s = new String('A',256).ToCharArray(); int sum = 0; for(int i=0; i<1000000000; i++) { int j = i&255; sum += s[j]; } //Read StringBuilder StringBuilder s= new StringBuilder(new String('A',256)); int sum = 0; for(int i=0; i<1000000000; i++) { int j = i&255; sum += s[j]; } 

(Yes, I know that my control code is not very good, but I don’t think it matters much.)

+13
source

The advantage of string arrays of char is that you can change character arrays in place; in C # lines are immutable, so any change creates a new object on the heap with a modified version of the line. In a char array, you can make many changes without allocating anything on the heap.

+1
source

Source: https://habr.com/ru/post/892491/


All Articles