program.cs

来自「这是.net2005学习不可缺少的教程」· CS 代码 · 共 82 行

CS
82
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace AdvStrStrB2
{
    class Program
    {
        const int numTicks = 10000000;

        static void Main(string[] args)
        {
            String s1 = "When I was young, I listen to the radio.";
            String s2;
            String s3;
            StringBuilder sb = new StringBuilder(s1);

            s2 = s1.Replace('.', '!');

            int startTime, endTime;
            int count;

            // Insert, Remove : String, Interned
            startTime = Environment.TickCount;
            for (count = 0; count < numTicks; count++)
            {
                s3 = s1.Insert(2, "really ");
                s1 = s3.Remove(2, 7);
            }
            endTime = Environment.TickCount;
            Console.WriteLine("Insert/Remove: Interned String " + (endTime - startTime));

            // INSERT : String, not Interned
            startTime = Environment.TickCount;
            for (count = 0; count < numTicks; count++)
            {
                s3 = s2.Insert(2, "really ");
                s2 = s3.Remove(2, 7);
            }
            endTime = Environment.TickCount;
            Console.WriteLine("Insert/Remove: NOT Interned String " + (endTime - startTime));
            
            // INSERT,Remove : StringBuilder
            startTime = Environment.TickCount;
            for (count = 0; count < numTicks; count++)
            {
                sb.Insert(2, "really ");
                sb.Remove(2, 7);
            }
            endTime = Environment.TickCount;
            Console.WriteLine("Insert/Remove: StringBuilder " + (endTime - startTime));

            // Replace : String, interned
            startTime = Environment.TickCount;
            for (count = 0; count < numTicks; count++)
            {
                s1 = s1.Replace("listen to the radio", "play football");
            }
            endTime = Environment.TickCount;
            Console.WriteLine("Replace: String, interned " + (endTime - startTime));

            // Replace : String, NOT interned
            startTime = Environment.TickCount;
            for (count = 0; count < numTicks; count++)
            {
                s2 = s2.Replace("listen to the radio", "play football");
            }
            endTime = Environment.TickCount;
            Console.WriteLine("Replace: String, NOT Interned " + (endTime - startTime));

            // Replace : StringBuilder
            startTime = Environment.TickCount;
            for (count = 0; count < numTicks; count++)
            {
                sb.Replace("listen to the radio", "play football");
            }
            endTime = Environment.TickCount;
            Console.WriteLine("Replace: StringBuilder " + (endTime - startTime));
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?