本文中测试了将多个字符串用指定的分隔符连接的几种方式,测试代码如下:
public static void Main()
{
ArrayList al = new ArrayList(10);
String[] strs = new string[]{"abcdefg", "gfdadfd", "adfasdf", "adfasdf", "adfaidf", "adfasdf", "adceadf", "adefadf", "adfadsf", "adfapsf"};
al.AddRange(strs);
String sep = ",";
DateTime dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Empty;
for (int i = 0; i < 10 ; i++)
{
s += sep;
s += al[i];
}
s = s.Substring(1);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < 10 ; i++)
{
sb.Append(sep);
sb.Append(al[i]);
}
string s = sb.ToString(1, sb.Length - 1);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10 ; i++)
{
sb.Append(sep);
sb.Append(al[i]);
}
string s = sb.ToString(1, sb.Length - 1);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
sb.Append(sep);
}
sb.Length -= 1;
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
sb.Append(sep);
}
sb.Length -= 1;
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Join(sep, strs);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Join(sep, (string[])al.ToArray(typeof(string)));
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string[] ss = new string[al.Count];
for (int i = 0; i < 10; i++)
{
ss[i] = al[i].ToString();
}
string s = String.Join(sep, ss);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
}
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
}
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Concat((string[])al.ToArray(typeof(string)));
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Concat(strs);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Concat(al.ToArray());
}
Console.WriteLine(DateTime.Now - dt);
}
{
ArrayList al = new ArrayList(10);
String[] strs = new string[]{"abcdefg", "gfdadfd", "adfasdf", "adfasdf", "adfaidf", "adfasdf", "adceadf", "adefadf", "adfadsf", "adfapsf"};
al.AddRange(strs);
String sep = ",";
DateTime dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Empty;
for (int i = 0; i < 10 ; i++)
{
s += sep;
s += al[i];
}
s = s.Substring(1);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < 10 ; i++)
{
sb.Append(sep);
sb.Append(al[i]);
}
string s = sb.ToString(1, sb.Length - 1);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10 ; i++)
{
sb.Append(sep);
sb.Append(al[i]);
}
string s = sb.ToString(1, sb.Length - 1);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
sb.Append(sep);
}
sb.Length -= 1;
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
sb.Append(sep);
}
sb.Length -= 1;
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Join(sep, strs);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Join(sep, (string[])al.ToArray(typeof(string)));
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string[] ss = new string[al.Count];
for (int i = 0; i < 10; i++)
{
ss[i] = al[i].ToString();
}
string s = String.Join(sep, ss);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
}
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10 ; i++)
{
sb.Append(al[i]);
}
string s = sb.ToString();
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Concat((string[])al.ToArray(typeof(string)));
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Concat(strs);
}
Console.WriteLine(DateTime.Now - dt);
dt = DateTime.Now;
for (int t = 0; t < 10000000; t++ )
{
string s = String.Concat(al.ToArray());
}
Console.WriteLine(DateTime.Now - dt);
}
测试结果如下:
- 直接使用string类进行连接,最后用Substring(1)去掉开头的分隔符,运行时间为 00:00:44.7643680
- 为StringBuilder指定刚好足够大的内存,用Append连接,最后用ToString(1, sb.Length - 1)去掉开头的分隔符,运行时间为 00:00:20.6396784
- 无法预知StringBuilder需要的内存数,不指定初始内存,使用默认值16,这样会造成三次内存重分配,用Append连接,最后用ToString(1, sb.Length - 1)去掉开头的分隔符,运行时间为 00:00:26.8486064
- 为StringBuilder指定刚好足够大的内存,用Append连接,最后用Length = Length - 1去掉结尾的分隔符,运行时间为 00:00:17.6553872
- 无法预知StringBuilder需要的内存数,不指定初始内存,使用默认值16,这样会造成三次内存重分配,用Append连接,最后用Length = Length - 1去掉结尾的分隔符,运行时间为 00:00:23.4336960
- 使用String.Join,参数为string[],运行时间为 00:00:07.2604400
- 使用String.Join,参数为包含string的ArrayList,调用ArrayList.ToArray得到字符串数组,运行时间为 00:00:19.6782960
- 使用String.Join,创建指定长度的string[],并将ArrayList的每一个元素调用ToString后保存到数组中,运行时间为 00:00:09.7740544
之后的代码仅测试字符串连接,不包含分隔符,结果如下:
- 为StringBuilder指定刚好足够大的内存,用Append连接,运行时间为 00:00:10.8856528
- 无法预知StringBuilder需要的内存数,不指定初始内存,使用默认值16,这样会造成三次内存重分配,用Append连接,运行时间为 00:00:18.0459488
- 使用String.Concat(string[]),参数为包含string的ArrayList,调用ArrayList.ToArray得到string[],运行时间为 00:00:20.1790160
- 使用String.Concat(string[]),参数为string[],运行时间为 00:00:07.5909152
- 使用String.Concat(object[]),参数为包含string的ArrayList,调用ArrayList.ToArray得到object[],运行时间为 00:00:12.2175680
不同的情况下,需要使用不同的方法以达到最好的效率,但是,我们应该清楚一点,并不是所有的场合StringBuilder都可以来代替string的操作,如果对string的操作是可预知的类型,并且string类本身提供了相应的功能,则应当优先使用string类的功能。
还有一个比较有意思的事,ArrayList.ToArray(Type)的效率要比ArrayList.ToArray()要差一些,可能指定Type的时候ArrayList要分配内存,还要进行有效性检查。