String connection Performance Comparison

Source: Internet
Author: User

Lao Zhao's character string performance series ~

Make a note ~ Http://www.cnblogs.com/JeffreyZhao/archive/2009/11/26/string-concat-perf-1-benchmark.html

Points of View:

  • When the number of strings is relatively small (about 5-6 in terms of data), the performance of stringbuilder is not faster than that of common connection operations. Therefore, it is inappropriate to use stringbuilder anywhere.
  • Although charlistbuilder "simulates" The stringbuilder situation, it is still much slower. It indicates that the internal implementation of stringbuilder is not as simple as imagined. (In our first imagination, if we look at "CLR via C #", we will find that the book tells us that the stringbuilder actually maintains a structure similar to the character array, in the process of continuously adding strings, this array will double the capacity as needed-isn't that the behavior of list <t> ?)
  • Although charlistbuilder is much slower than stringbuilder, it is still far ahead of common string connection operations in the case of a large number of strings. This shows that the practice of continuously generating new strings is indeed a performance killer.
  • When the number of strings is determined, the performance of string. Concat is higher than that of stringbuilder. In fact, the larger the number of strings, the more obvious the gap.

    Why is string. Concat so fast?

    This is the full implementation of the string. Concat (string []) method, which is very simple and clear, but this is also the reason for the efficiency of this method. We know that a string is an immutable object. Every time you create a new string, a new space is required. The string. Concat method minimizes the cost of opening up new spaces. Because the size of the result has been determined before, you can directly create a "container", and the rest is to fill the data. Since there is no waste of space and no additional operations, how can the performance not be high?

    • Stringbuilder: If you can determine the final length of the target string, you can use stringbuilder. If you are not sure, you can also specify a larger capacity at the beginning to reduce the number of resizing times.
    Code

    Private Static string newstringbuilder (INT count)
    {
    VaR builder = new stringbuilder (count * Str. Length );
    For (INT I = 0; I <count; I ++)
    Builder. append (STR );
    Return builder. tostring ();
    }
    • String. Concat: if you cannot determine the final length, but you can determine the number of strings (such as in this scenario), you can put them in an array and call String. Concat to connect.
    Code

    Private Static string stringconcat (INT count)
    {
    VaR array = new string [count];
    For (INT I = 0; I <count; I ++) array [I] = STR;
    Return string. Concat (array );
    }

    • Stringlistbuilder: a compromise. Its advantage over string. Concat is that you do not need to determine the number of strings. Compared with stringbuilder, you only need to copy some references for the scale-up operation.
    Code

    Public class stringlistbuilder
    {
    Private list <string> m_list;

    Public stringlistbuilder (INT capacity)
    {
    This. m_list = new list <string> (capacity );
    }

    Public stringlistbuilder append (string S)
    {
    This. m_list.add (s );
    Return this;
    }

    Public String tostring ()
    {
    Return string. Concat (this. m_list.toarray ());
    }
    }

     

     

    Code

    Private Static string stringlistbuilder (INT count)
    {
    VaR builder = new stringlistbuilder (count );
    For (INT I = 0; I <count; I ++) builder. append (STR );
    Return builder. tostring ();
    }

     

     

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.