问题描述
我已经在这里工作了大约一个月的一些程序,这些程序有很多字符串解析等等.有人建议我对这些东西使用 char 数组而不是字符串,因为 char 数组更快.我理解为什么 char 数组很快,但它是什么让字符串类型变慢?它实现了什么数据结构,有什么方法可以让它像 char 数组一样快?
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?
推荐答案
最明显的区别是 string
是不可变的.所以你不能修改它的一部分并且需要在每次修改时创建一个全新的副本.
The most obvious difference is that string
is immutable. So you can't modify parts of it and need to create a completely new copy on each modification.
String 本身有一个非常特殊的实现(它是一个可变大小的类)并且没有数组支持.我看不出为什么对字符串中的 char
s 的只读访问应该很慢.
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 char
s in a string should be slow.
因此,如果您想更改字符串的一小部分,您需要使用 StringBuilder
或 char[]
.这两个 char[]
是/曾经更快,因为 StringBuilder
有额外的验证和间接.但由于这是一个实现细节,自从我上次测试以来它可能已经改变了.
So if you want to change small parts of a string, you need to use either StringBuilder
or char[]
. Of these two char[]
is/was faster since StringBuilder
has additional verifications and indirections. But since this is an implementation detail it might have changed since I last tested it.
刚刚对其进行了基准测试,从 .NET 4 开始,设置 char[]
的成员的速度大约是 StringBuilder
的四倍.但两者都可以每秒执行超过 2 亿次任务,因此在实践中几乎不重要.
Just benchmarked it, and as of .NET 4 setting a member of char[]
is about four times as fast compared to a StringBuilder
. But both can do more than 200 milion assignments per second, so it rarely matters in practice.
从 char[]
读取比从 string
读取稍快(我的测试代码为 25%).另一方面,从 StringBuilder
读取比从 char[]
读取要慢(3 倍).
Reading from a char[]
is slightly faster (25% for my test code) that reading from string
. Reading from StringBuilder
on the other hand is slower (a factor of 3) than reading from char[]
.
在所有基准测试中,我都忽略了其他代码的开销.这意味着我的测试有点低估了差异.
In all benchmarks I neglected the overhead of my other code. This means that my test underestimates the differences a bit.
我的结论是,虽然 char[]
比其他替代品更快,但它仅在您每秒处理数百兆字节时才重要.
My conclusion is that while char[]
is faster than the alternatives it only matters if you're going over 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 my benchmark code isn't very good, but I don't think it makes a big difference.)
这篇关于字符串类型 .NET 与 char 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!