[VB.NET Tips] talk concatenation of built-pool

CLR automatically maintain a table called "built-pool" (the scratch pool) (intern pool) is, at compile time This table contains a single instance of each unique string constants declared in the program, and to programmatically create a String the only instance of any class.
Built-in pool is implemented as a hash table. It means that using a hash table, a character string can be represented by a number or "hash code." This is more and the search string is very effective, because it is not comparing strings character by character, but merely compare the hash value.
Built-in string pool to save storage space. If a string is assigned several variables constant, the same for each variable reference constant pool built, rather than several different instances of reference String class have the same value.

The code may be tested as follows:


    Const APPNAME As String = "运维应用程序"

    Console.WriteLine(String.IsInterned(APPNAME) <> Nothing)

    Dim s1 As String
    Dim s2 As String

    s1 = "这是一个测试的字符串"
    s2 = "这是一个测试的字符串"


    Console.WriteLine(String.IsInterned(s1) <> Nothing)
    Console.WriteLine("s1的散列值:{0}", s1.GetHashCode())
    Console.WriteLine("s2的散列值:{0}", s2.GetHashCode())

    Dim s3 As String = s1 & DateTime.Now().ToString()
    Console.WriteLine("s3的散列值:{0}", s3.GetHashCode())
    Console.WriteLine(String.IsInterned(s3) <> Nothing)

Run the following code will find that .NET will be built string containing the string constants, and is created programmatically.
S1 and s2 will find references to the same string instance. s3 will not be built, and can not confirm the details because s3 at compile time. You can also use the built-String.Intern to force a variable.
With plans to say something:

So what good is it built, compare the following code, the second method more than 3 times faster than the first method.


     Sub Main()
        Console.Write("请输入你的名字:")
        Dim userName As String = Console.ReadLine()

        Method1(userName)
        Method2(userName)

        Console.Read()

    End Sub

    Public Sub Method1(ByVal username As String)

        Dim strHTML As String

        '每次循环创建3次字符串,分配3次内存
        Dim startms As Integer = Environment.TickCount()
        For i As Integer = 1 To 1000000
            strHTML = "<HTML><HEAD></HEAD><BODY>"
            strHTML &= "我的名字是:"
            strHTML &= username
            strHTML &= "</BODY></HTML>"
        Next
        Dim endms As Integer = Environment.TickCount()

        Console.WriteLine("总耗时:{0}微秒", (endms - startms))

    End Sub
    Public Sub Method2(ByVal username As String)

        Dim strHTML As String
        Const strPrefix As String = "<HTML><HEAD></HEAD><BODY>"
        Const strSuffix As String = "</BODY></HTML>"

        '只创建一次字符串
        Dim startms As Integer = Environment.TickCount()
        For i As Integer = 1 To 1000000
            strHTML = strPrefix & "请输入你的名字:" & userName & strSuffix
        Next
        Dim endms As Integer = Environment.TickCount()

        Console.WriteLine("总耗时:{0}微秒", (endms - startms))

    End Sub

But to stimulate the above method, flexible application in practical applications. A large number of strings in the web splicing operation of applications may be built using multiple. After all, fast processing speed, higher concurrency can handle.

Guess you like

Origin www.cnblogs.com/tengwei6328/p/11415692.html