• <i id='RlgyM'><tr id='RlgyM'><dt id='RlgyM'><q id='RlgyM'><span id='RlgyM'><b id='RlgyM'><form id='RlgyM'><ins id='RlgyM'></ins><ul id='RlgyM'></ul><sub id='RlgyM'></sub></form><legend id='RlgyM'></legend><bdo id='RlgyM'><pre id='RlgyM'><center id='RlgyM'></center></pre></bdo></b><th id='RlgyM'></th></span></q></dt></tr></i><div id='RlgyM'><tfoot id='RlgyM'></tfoot><dl id='RlgyM'><fieldset id='RlgyM'></fieldset></dl></div>

    <small id='RlgyM'></small><noframes id='RlgyM'>

    • <bdo id='RlgyM'></bdo><ul id='RlgyM'></ul>

        <tfoot id='RlgyM'></tfoot>
        <legend id='RlgyM'><style id='RlgyM'><dir id='RlgyM'><q id='RlgyM'></q></dir></style></legend>

        字符串类型 .NET 与 char 数组

        时间:2023-05-20

        <small id='abT1b'></small><noframes id='abT1b'>

        <tfoot id='abT1b'></tfoot>
          <tbody id='abT1b'></tbody>
        <i id='abT1b'><tr id='abT1b'><dt id='abT1b'><q id='abT1b'><span id='abT1b'><b id='abT1b'><form id='abT1b'><ins id='abT1b'></ins><ul id='abT1b'></ul><sub id='abT1b'></sub></form><legend id='abT1b'></legend><bdo id='abT1b'><pre id='abT1b'><center id='abT1b'></center></pre></bdo></b><th id='abT1b'></th></span></q></dt></tr></i><div id='abT1b'><tfoot id='abT1b'></tfoot><dl id='abT1b'><fieldset id='abT1b'></fieldset></dl></div>

            <bdo id='abT1b'></bdo><ul id='abT1b'></ul>

              <legend id='abT1b'><style id='abT1b'><dir id='abT1b'><q id='abT1b'></q></dir></style></legend>
                1. 本文介绍了字符串类型 .NET 与 char 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我已经在这里工作了大约一个月的一些程序,这些程序有很多字符串解析等等.有人建议我对这些东西使用 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 本身有一个非常特殊的实现(它是一个可变大小的类)并且没有数组支持.我看不出为什么对字符串中的 chars 的只读访问应该很慢.

                  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.

                  因此,如果您想更改字符串的一小部分,您需要使用 StringBuilderchar[].这两个 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 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:.NET System::String 到存储在 char* 中的 UTF8 字节 下一篇:C#中字符串中的char *指针

                  相关文章

                  <legend id='0cBZk'><style id='0cBZk'><dir id='0cBZk'><q id='0cBZk'></q></dir></style></legend>

                      <i id='0cBZk'><tr id='0cBZk'><dt id='0cBZk'><q id='0cBZk'><span id='0cBZk'><b id='0cBZk'><form id='0cBZk'><ins id='0cBZk'></ins><ul id='0cBZk'></ul><sub id='0cBZk'></sub></form><legend id='0cBZk'></legend><bdo id='0cBZk'><pre id='0cBZk'><center id='0cBZk'></center></pre></bdo></b><th id='0cBZk'></th></span></q></dt></tr></i><div id='0cBZk'><tfoot id='0cBZk'></tfoot><dl id='0cBZk'><fieldset id='0cBZk'></fieldset></dl></div>
                        <bdo id='0cBZk'></bdo><ul id='0cBZk'></ul>

                      <tfoot id='0cBZk'></tfoot>
                    1. <small id='0cBZk'></small><noframes id='0cBZk'>