String.Join vs. StringBuilder:哪个更快?

String.Join vs. StringBuilder:哪个更快?

在之前关于格式化double[][]为CSV格式的问题中,有人建议使用StringBuilder比使用更快的格式String.Join。这是真的?



白板的微信
浏览 765回答 3
3回答

慕桂英4014372

简答:这取决于。答案很长:如果你已经有一个字符串数组连在一起(用分隔符),这String.Join是最快的方法。String.Join可以查看所有字符串以计算出所需的确切长度,然后再次复制并复制所有数据。这意味着不会涉及额外的复制。该唯一的缺点是,它要经过串的两倍,这意味着潜在吹内存缓存更多的时间比必要的。如果您事先没有将字符串作为数组,那么它的使用速度可能会更快StringBuilder- 但是会出现这种情况。如果使用StringBuilder手段做很多很多副本,那么构建一个数组然后调用String.Join可能会更快。编辑:这是String.Join对一连串调用的一次调用StringBuilder.Append。在最初的问题中,我们有两个不同级别的String.Join调用,因此每个嵌套调用都会创建一个中间字符串。换句话说,它更复杂,更难以猜测。我会惊讶地发现,与典型数据相比,(在复杂性方面)要么“获胜”。编辑:当我在家时,我会写一个尽可能痛苦的基准StringBuilder。基本上如果你有一个数组,其中每个元素大小是前一个元素的两倍,并且你得到它恰到好处,你应该能够为每个附加强制复制(元素,而不是分隔符,尽管这需要也要考虑到)。那时它几乎和简单的字符串连接一样糟糕 - 但是String.Join没有问题。

隔江千里

我不这么认为。透过Reflector看,执行效果String.Join非常优化。它还具有预先知道要创建的字符串总大小的额外好处,因此不需要任何重新分配。我创建了两种测试方法来比较它们:public static string TestStringJoin(double[][] array){&nbsp; &nbsp; return String.Join(Environment.NewLine,&nbsp; &nbsp; &nbsp; &nbsp; Array.ConvertAll(array,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row => String.Join(",",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Array.ConvertAll(row, x => x.ToString()))));}public static string TestStringBuilder(double[][] source){&nbsp; &nbsp; // based on Marc Gravell's code&nbsp; &nbsp; StringBuilder sb = new StringBuilder();&nbsp; &nbsp; foreach (var row in source)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (row.Length > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.Append(row[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < row.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.Append(',').Append(row[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return sb.ToString();}我运行了每个方法50次,传入一个大小的数组[2048][64]。我为两个阵列做了这个; 一个填充零,另一个填充随机值。我在我的机器上得到了以下结果(P4 3.0 GHz,单核,无HT,从CMD运行释放模式):// with zeros:TestStringJoin&nbsp; &nbsp; took 00:00:02.2755280TestStringBuilder took 00:00:02.3536041// with random values:TestStringJoin&nbsp; &nbsp; took 00:00:05.6412147TestStringBuilder took 00:00:05.8394650增加数组的大小[2048][512],同时将迭代次数减少到10得到以下结果:// with zeros:TestStringJoin&nbsp; &nbsp; took 00:00:03.7146628TestStringBuilder took 00:00:03.8886978// with random values:TestStringJoin&nbsp; &nbsp; took 00:00:09.4991765TestStringBuilder took 00:00:09.3033365结果是可重复的(几乎;由不同的随机值引起的小波动)。显然String.Join大部分时间都要快一点(虽然幅度非常小)。这是我用于测试的代码:const int Iterations = 50;const int Rows = 2048;const int Cols = 64; // 512static void Main(){&nbsp; &nbsp; OptimizeForTesting(); // set process priority to RealTime&nbsp; &nbsp; // test 1: zeros&nbsp; &nbsp; double[][] array = new double[Rows][];&nbsp; &nbsp; for (int i = 0; i < array.Length; ++i)&nbsp; &nbsp; &nbsp; &nbsp; array[i] = new double[Cols];&nbsp; &nbsp; CompareMethods(array);&nbsp; &nbsp; // test 2: random values&nbsp; &nbsp; Random random = new Random();&nbsp; &nbsp; double[] template = new double[Cols];&nbsp; &nbsp; for (int i = 0; i < template.Length; ++i)&nbsp; &nbsp; &nbsp; &nbsp; template[i] = random.NextDouble();&nbsp; &nbsp; for (int i = 0; i < array.Length; ++i)&nbsp; &nbsp; &nbsp; &nbsp; array[i] = template;&nbsp; &nbsp; CompareMethods(array);}static void CompareMethods(double[][] array){&nbsp; &nbsp; Stopwatch stopwatch = Stopwatch.StartNew();&nbsp; &nbsp; for (int i = 0; i < Iterations; ++i)&nbsp; &nbsp; &nbsp; &nbsp; TestStringJoin(array);&nbsp; &nbsp; stopwatch.Stop();&nbsp; &nbsp; Console.WriteLine("TestStringJoin&nbsp; &nbsp; took " + stopwatch.Elapsed);&nbsp; &nbsp; stopwatch.Reset(); stopwatch.Start();&nbsp; &nbsp; for (int i = 0; i < Iterations; ++i)&nbsp; &nbsp; &nbsp; &nbsp; TestStringBuilder(array);&nbsp; &nbsp; stopwatch.Stop();&nbsp; &nbsp; Console.WriteLine("TestStringBuilder took " + stopwatch.Elapsed);}static void OptimizeForTesting(){&nbsp; &nbsp; Thread.CurrentThread.Priority = ThreadPriority.Highest;&nbsp; &nbsp; Process currentProcess = Process.GetCurrentProcess();&nbsp; &nbsp; currentProcess.PriorityClass = ProcessPriorityClass.RealTime;&nbsp; &nbsp; if (Environment.ProcessorCount > 1) {&nbsp; &nbsp; &nbsp; &nbsp; // use last core only&nbsp; &nbsp; &nbsp; &nbsp; currentProcess.ProcessorAffinity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = new IntPtr(1 << (Environment.ProcessorCount - 1));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP