字符串连接字符串VS生成器。性能 [英] String concatenation vs String Builder. Performance

查看:171
本文介绍了字符串连接字符串VS生成器。性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要连接多个字符串,形成一个类的ID。基本上,我只是在列表中循环,以获得对象的ToString值,然后连接它们。

 的foreach(在myList中为MyObject O)
  结果+ = o.ToString();

该列表预计不会有超过5种元素(虽然它可以,但是这是一个非常,非常边缘的情况下),通常会有1到3元,是常见的它只是有一个或两个。

什么是更高的性能,保持串联或使用StringBuilder的?

  StringBuilder的BLD =新的StringBuilder()
的foreach(在myList中为MyObject O)
  bld.Append(o.ToString());

我不确定如果创建的StringBuilder将采取比最常见的情况的标准串联更多的时间。

这是懒惰的,不换一次创建的列表上的项目所谓将该ID懒洋洋地构建了一次。

作为一个侧面说明...我应该使用固定的阵列,而不是一个列表?我会得到任何性能或改善记忆,如果我做什么? (名单只作为一个IEnumerable反正)

问题的更普遍的看法可能是,许多字符串怎么都不足以阻止串联,并开始建立?

我应该甚至懒得测试用例情景?

 如果(myList.Count> 4)
  ConcatWithStringBuilder(myList中);


解决方案

通常的答案是,字符串连接的是8串4之间更有效。这取决于他们的博客你看了。

不要写一个测试,以确定哪种方法使用。如果你不确定它是否会在魔法的限制,那么就使用StringBuilder的。

运行此code看到的结果自己:

  const int的SLEN = 30,循环= 5000;
日期时间STIME,ETIME;
INT I;
字符串sSource =新的String(X,SLEN);
字符串sDest =;
//
//时间字符串连接。
//
STIME = DateTime.Now;
对于(i = 0; I<循环;我++)sDest + = sSource;
ETIME = DateTime.Now;
Console.WriteLine(串联了+(ETIME - STIME).TotalSeconds +秒);
//
//时间的StringBuilder。
//
STIME = DateTime.Now;
System.Text.StringBuilder SB =新System.Text.StringBuilder((INT)(SLEN *循环* 1.1));
对于(i = 0; I<循环;我++)sb.Append(sSource);
sDest = sb.ToString();
ETIME = DateTime.Now;
Console.WriteLine(字符串构建器带+(ETIME - STIME).TotalSeconds +秒);
//
//使控制台窗口继续开放
//这样就可以看到结果从IDE中运行时。
//
Console.WriteLine();
Console.Write(preSS Enter完成...);
Console.Read();

参考。 http://support.microsoft.com/kb/306822

I have a situation where I need to concatenate several string to form an id of a class. Basically I'm just looping in a list to get the ToString values of the objects and then concatenating them.

foreach (MyObject o in myList)
  result += o.ToString();

The list is NOT expected to have more than 5 elements (although it could but that's a very, very marginal case) and usually will have from 1 to 3 elements, being common for it to just have one or two.

What would be more performance, keeping the concatenation or using an StringBuilder?

StringBuilder bld = new StringBuilder()
foreach (MyObject o in myList)
  bld.Append(o.ToString());

I'm unsure if creating the StringBuilder will take more time than standard concatenation for the most usual case.

This is lazy, items on the list do not change once created so the id is lazily constructed once when called.

As a side note... Should I use a fixed array instead of a List? Would I get any performance or memory improvement if I do? (List is only used as an IEnumerable anyway)

A more general view of the question could be, how many strings are enough to stop concatenating and start building?

Should I even bother to test case the scenario?

if (myList.Count > 4) 
  ConcatWithStringBuilder(myList);

解决方案

The usual answer is that string concatenation is more efficient for between 4 to 8 strings. It depends on whose blog you read.

Don't write a test to decide on which method to use. If you are unsure of whether it will go over the magic limit, then just use StringBuilder.

Run this code to see the results for yourself:

const int sLen=30, Loops=5000;
DateTime sTime, eTime;
int i;
string sSource = new String('X', sLen);
string sDest = "";
// 
// Time string concatenation.
// 
sTime = DateTime.Now;
for(i=0;i<Loops;i++) sDest += sSource;
eTime = DateTime.Now;
Console.WriteLine("Concatenation took " + (eTime - sTime).TotalSeconds + " seconds.");
// 
// Time StringBuilder.
// 
sTime = DateTime.Now;
System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));
for(i=0;i<Loops;i++) sb.Append(sSource);
sDest = sb.ToString();
eTime = DateTime.Now;
Console.WriteLine("String Builder took " + (eTime - sTime).TotalSeconds + " seconds.");
// 
// Make the console window stay open
// so that you can see the results when running from the IDE.
// 
Console.WriteLine();
Console.Write("Press Enter to finish ... ");
Console.Read();

Ref. http://support.microsoft.com/kb/306822

这篇关于字符串连接字符串VS生成器。性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆