String和StringBuilder的区别

            //创建一个控制台应用程序,在Mian()方法中编写如下代码,验证字符串操作和可变字符串操作的执行效率。
            string str = "";
            long longStartTime = DateTime.Now.Millisecond;
            for (int i = 0; i < 10000; i++)
            {
                str = str + i;
            }
            long longEndTime = DateTime.Now.Millisecond;
            long time = longEndTime - longStartTime;
            Console.WriteLine(time);

            StringBuilder str1 = new StringBuilder();
            longStartTime = DateTime.Now.Millisecond;
            for (int j = 0; j < 10000; j++)
            {
                str1.Append(j);
            }
            longEndTime = DateTime.Now.Millisecond;
            time = longEndTime - longStartTime;
            Console.WriteLine(time);

            Console.ReadLine();

 

posted @ 2024-08-18 18:49  CSF践行  阅读(7)  评论(0编辑  收藏  举报