C# 少量字符串链接方法及测试(2)字符串与值对象

字符串连接方式

  • str1 + i
  • $”str1{i}” (C#7.0新增)
  • String.Format
  • String.Concat
  • StringBuilder

测试方法及结果

测试方法

  1. 代表使用每种方法分别连接一个字符串和一个数字,
  2. 使用每种方法分别连接一个字符串和一个数字的ToString(避免装箱拆箱)
  3. 代表使用每种方法分别连接两个字符串和两个数字,
  4. 使用每种方法分别连接两个字符串和两个数字的ToString(避免装箱拆箱)
  5. 代表使用每种方法分别连接10个字符串和10个数字,
  6. 使用每种方法分别连接10个字符串和10个数字的ToString(避免装箱拆箱)
  7. 测试环境为VS2017,循环10000000次并计时

结论

在处理少量字符串链接与值类型

  1. 推荐用 + 配合ToString()易读性好,性能损失不大。
  2. Concat 配合ToString() 性能最优, $”str1{i}”与Format 性能最差。
  3. 装箱拆箱对 + 和 Concat 的影响比较大。
  4. string builder 在固定数量的常量与值对象链接时并不占优。

测试结果

测试1
- Link one string and one number.
- plus 1: 1581ms
- plus 2: 925ms
- 1:2139ms 2: 1976ms
- format 1: 2152ms
- format 2: 1981ms
- concat 1: 1006ms
- concat 2: 900ms
- String builder 1: 1262ms
- String builder 2: 1263ms
- Link 2 string and 2 number.
- plus 1: 2528ms
- plus 2: 1818ms
- 1:3531ms 2: 3898ms
- format 1: 3793ms
- format 2: 3377ms
- concat 1: 2630ms
- concat 2: 1815ms
- String builder 1: 2554ms
- String builder 2: 2880ms
- Link 10 strings and 10 numbers.
- plus 1: 12517ms
- plus 2: 9429ms
- 1:14904ms 2: 14110ms
- format 1: 16293ms
- format 2: 14715ms
- concat 1: 12494ms
- concat 2: 9422ms
- String builder 1: 12982ms
- String builder 2: 12579ms

测试2
- format 1: 2658ms
- format 2: 2237ms
- concat 1: 1056ms
- concat 2: 960ms
- String builder 1: 1224ms
- String builder 2: 1277ms
- Link 2 string and 2 number.
- plus 1: 2633ms
- plus 2: 1862ms
- 1:3767ms 2: 3480ms
- format 1: 3739ms
- format 2: 3443ms
- concat 1: 2690ms
- concat 2: 1829ms
- String builder 1: 2641ms
- String builder 2: 2639ms
- Link 10 strings and 10 numbers.
- plus 1: 13703ms
- plus 2: 10490ms
- 1:16663ms 2: 15077ms
- format 1: 18308ms
- format 2: 15590ms
- concat 1: 14043ms
- concat 2: 10376ms
- String builder 1: 14037ms
- String builder 2: 14303ms

Code

            var str = "";
            var sw = new System.Diagnostics.Stopwatch();
            Console.WriteLine("Link one string and one number.");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = "p1 i:" + i;
            }

            sw.Stop();
            Console.WriteLine("plus 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = "p2 i:" + i.ToString();
            }

            sw.Stop();
            Console.WriteLine("plus 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = $"$1 i:{i}";
            }

            sw.Stop();
            Console.WriteLine("$ 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = $"$2 i:{i.ToString()}";
            }

            sw.Stop();
            Console.WriteLine("$ 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Format("f1 i:{0}", i);
            }

            sw.Stop();
            Console.WriteLine("format 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Format("f2 i:{0}", i.ToString());
            }

            sw.Stop();
            Console.WriteLine("format 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Concat("c1 i:", i);
            }

            sw.Stop();
            Console.WriteLine("concat 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Concat("c2 i:", i.ToString());
            }

            sw.Stop();
            Console.WriteLine("concat 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                var builder = new System.Text.StringBuilder();
                builder.Append("b1 i:");
                builder.Append(i);
                str = builder.ToString();
            }

            sw.Stop();
            Console.WriteLine("String builder 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                var builder = new System.Text.StringBuilder();
                builder.Append("b2 i:");
                builder.Append(i.ToString());
                str = builder.ToString();
            }

            sw.Stop();
            Console.WriteLine("String builder 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            Console.WriteLine("Link 2 string and 2 number.");
            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = "p1 i1:" + i + ", i2:" + i;
            }

            sw.Stop();
            Console.WriteLine("plus 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = "p2 i1:" + i.ToString() + ", i2:" + i.ToString();
            }

            sw.Stop();
            Console.WriteLine("plus 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = $"$1 i1:{i}, i2:{i}";
            }

            sw.Stop();
            Console.WriteLine("$ 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = $"$2 i1:{i.ToString()}, i2:{i.ToString()}";
            }

            sw.Stop();
            Console.WriteLine("$ 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Format("f1 i1:{0}, i2:{1}", i, i);
            }

            sw.Stop();
            Console.WriteLine("format 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Format("f2 i1:{0}, i2:{1}", i.ToString(), i.ToString());
            }

            sw.Stop();
            Console.WriteLine("format 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Concat("c1 i1:", i, ", i2:", i);
            }

            sw.Stop();
            Console.WriteLine("concat 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                str = String.Concat("c2 i1:", i.ToString(), ", i2:", i.ToString());
            }

            sw.Stop();
            Console.WriteLine("concat 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                var builder = new System.Text.StringBuilder();
                builder.Append("b1 i1:");
                builder.Append(i);
                builder.Append(", i2:");
                builder.Append(i);
                str = builder.ToString();
            }

            sw.Stop();
            Console.WriteLine("String builder 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 10000000; i++)
            {
                var builder = new System.Text.StringBuilder();
                builder.Append("b2 i1:");
                builder.Append(i.ToString());
                builder.Append(", i2:");
                builder.Append(i.ToString());
                str = builder.ToString();
            }

            sw.Stop();
            Console.WriteLine("String builder 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            Console.WriteLine("Link 10 strings and 10 numbers.");
            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000;)
            {
                str = "p1 i1:" + i++ + ", i2:" + i++ + ", i3:" + i++ + ",i4:" + i++ + ", i5:" + i++ + ", i6:" + i++ + ", i7:" + i++ + ", i8:" + i++ + ", i9:" + i++ + ", i10:" + i++;
            }

            sw.Stop();
            Console.WriteLine("plus 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i++ < 100000000;)
            {
                str = "p2 i1:" + i++.ToString() + ", i2:" + i++.ToString() + ", i3:" + i++.ToString() + ", i4:" + i++.ToString() + ", i5:" + i++.ToString() + ", i6:" + i++.ToString() + ", i7:" + i++.ToString() + ", i8:" + i++.ToString() + ", i9:" + i++.ToString() + ", i10:" + i++.ToString();
            }

            sw.Stop();
            Console.WriteLine("plus 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000; i++)
            {
                str = $"$1 i1:{i++}, i2:{i++}, i3:{i++},i4:{i++}, i5:{i++}, i6:{i++}, i7:{i++}, i8:{i++}, i9:{i++}, i10:{i++}";
            }

            sw.Stop();
            Console.WriteLine("$ 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000; i++)
            {
                str = $"$2 i1:{i++.ToString()}, i2:{i++.ToString()}, i3:{i++.ToString()},i4:{i++.ToString()}, i5:{i++.ToString()}, i6:{i++.ToString()}, i7:{i++.ToString()}, i8:{i++.ToString()}, i9:{i++.ToString()}, i10:{i++.ToString()}";
            }

            sw.Stop();
            Console.WriteLine("$ 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000;)
            {
                str = String.Format("f1 i1:{0}, i2:{1}, i3:{2}, i4:{3}, i5:{4}, i6:{5}, i7:{6}, i8:{7}, i9:{8}, i10:{9}", i++, i++, i++, i++, i++, i++, i++, i++, i++, i++);
            }

            sw.Stop();
            Console.WriteLine("format 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000;)
            {
                str = String.Format("f2 i1:{0}, i2:{1}, i3:{2}, i4:{3}, i5:{4}, i6:{5}, i7:{6}, i8:{7}, i9:{8}, i10:{9}", i++.ToString(), i++.ToString(), i++.ToString(), i++.ToString(), i++.ToString(), i++.ToString(), i++.ToString(), i++.ToString(), i++.ToString(), i++.ToString());
            }

            sw.Stop();
            Console.WriteLine("format 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000;)
            {
                str = String.Concat("c1 i1:", i++, ", i2:", i++, ", i3:", i++, ",i4:", i++, ", i5:", i++, ", i6:", i++, ", i7:", i++, ", i8:", i++, ", i9:", i++, ", i10:", i++);
            }

            sw.Stop();
            Console.WriteLine("concat 1: " + sw.ElapsedMilliseconds.ToString() + "ms");
            sw.Reset();

            sw.Start();
            for (int i = 0; i++ < 100000000;)
            {
                str = String.Concat("c2 i1:", i++.ToString(), ", i2:", i++.ToString(), ", i3:", i++.ToString(), ",i4:", i++.ToString(), ", i5:", i++.ToString(), ", i6:", i++.ToString(), ", i7:", i++.ToString(), ", i8:", i++.ToString(), ", i9:", i++.ToString(), ", i10:", i++.ToString());
            }

            sw.Stop();
            Console.WriteLine("concat 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000;)
            {
                var builder = new System.Text.StringBuilder();
                builder.Append("p1 i1:");
                builder.Append(i++);
                builder.Append(", i2:");
                builder.Append(i++);
                builder.Append(", i3:");
                builder.Append(i++);
                builder.Append(", i4:");
                builder.Append(i++);
                builder.Append(", i5:");
                builder.Append(i++);
                builder.Append(", i6:");
                builder.Append(i++);
                builder.Append(", i7:");
                builder.Append(i++);
                builder.Append(", i8:");
                builder.Append(i++);
                builder.Append(", i9:");
                builder.Append(i++);
                builder.Append(", i10:");
                builder.Append(i++);
                str = builder.ToString();
            }

            sw.Stop();
            Console.WriteLine("String builder 1: " + sw.ElapsedMilliseconds.ToString() + "ms");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < 100000000;)
            {
                var builder = new System.Text.StringBuilder();
                builder.Append("p1 i1:");
                builder.Append(i++.ToString());
                builder.Append(", i2:");
                builder.Append(i++.ToString());
                builder.Append(", i3:");
                builder.Append(i++.ToString());
                builder.Append(", i4:");
                builder.Append(i++.ToString());
                builder.Append(", i5:");
                builder.Append(i++.ToString());
                builder.Append(", i6:");
                builder.Append(i++.ToString());
                builder.Append(", i7:");
                builder.Append(i++.ToString());
                builder.Append(", i8:");
                builder.Append(i++.ToString());
                builder.Append(", i9:");
                builder.Append(i++.ToString());
                builder.Append(", i10:");
                builder.Append(i++.ToString());
                str = builder.ToString();
            }

            sw.Stop();
            Console.WriteLine("String builder 2: " + sw.ElapsedMilliseconds.ToString() + "ms");

1

你可能感兴趣的:(C#,c#,string,contact,stringbuilder)