C#入门篇6-4:字符串操作 string分割字符串效率比较

       //分割字符串效率比较
        public static void Fund()
        {
            //1.用string.Split方法
            //a.字节数组:
            //625毫秒/百万次

            string str1 = "acabcdeabcdeabcde";
            string[] strArr1 = null;

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            for (int i = 0; i < 10000000; i++)
            {
                strArr1 = str1.Split(new char[1] { 'c' });
            }

            sw.Stop();

            Console.WriteLine(sw.ElapsedMilliseconds);//以毫秒为单位

            //b.字符串数组

            //703毫秒/百万次
            string str3 = "acabcdeabcdeabcde";
            string[] strArr3 = str3.Split(new string[1] { "cde" }, StringSplitOptions.None);

            //2.Regex.Split方法
            //7093毫秒/百万次
            string str4 = "acabcdeabcdeabcde";
            string[] strArr4 = Regex.Split(str4, "cde");
        }

 

你可能感兴趣的:(String)