三种字符串赋值方式性能对比

本文通过三种不同的字符串拼接方法(直接拼接、字符数组复制和StringBuilder)进行性能对比测试,结果显示StringBuilder方法性能最优。
        static void Main(string[] args)
        {
            for (int j = 0; j < 5; j++)
            {
		//第一种方法
                Stopwatch watch = new Stopwatch();
                watch.Start();
                string output;
                string strB = default(string);
                for (int i = 0; i < 100000000; i++)
                {
                    strB = "Good morning, Good morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morning";
                    strB += "world";
                }
                output = strB;
                watch.Stop();
                TimeSpan time = watch.Elapsed;
                Console.WriteLine("time new:" + time);
 		
		//第二种方法:先开辟一定的空间,然后向里面赋值
                Stopwatch watchA = new Stopwatch();
                watchA.Start();
                string strValue = "Good morning, Good morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morningGood morning";
                string strWorld = "world";
                char[] array = new char[1024];
                for (int i = 0; i < 100000000; i++)
                {
                    strValue.CopyTo(0, array, 0, strValue.Length);
                    strWorld.CopyTo(0, array, strValue.Length, strWorld.Length);
                }
                output = new string(array);
                watchA.Stop();
                TimeSpan timeA = watchA.Elapsed;
                Console.WriteLine("time array:" + timeA);
 		
		//第三种方法:StringBuilder方式
                Stopwatch watchB = new Stopwatch();
                watchB.Start();
                StringBuilder strValue1 = new StringBuilder(1024);
                for (int i = 0; i < 100000000; i++)
                {
                    strValue1.Clear();
                    strValue1.Append(strValue);
                    strValue1.Append(strWorld);
                }
                output = strValue1.ToString();
                watchB.Stop();
                TimeSpan timeB = watchB.Elapsed;
                Console.WriteLine("time stringBuilder:" + timeB);
                Console.WriteLine();
            }
        }
	结果如附图:
	
	从上面的结果可以看出,stringBuilder性能更优越

        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值