C# String address, splicing performance learning

  •  The String type is immutable. When a string variable is defined, storage space is allocated on the heap, and when the value of the variable is changed, a storage space is reallocated, and the original storage space is retained.

  Test idea: Obtain the storage space address before and after the value of the string type variable is changed, and determine whether the address is the same.

      Get the reference type address code:     

        public static string GetMemory(Object o) 
        {
            GCHandle h = GCHandle.Alloc(o, GCHandleType.Pinned);
            IntPtr addr = h.AddrOfPinnedObject();
            return "0x" + addr.ToString("X");
        }

      Test code:

            string str= "hello";
            Console.WriteLine(GetMemory(str));
            str = "hi";
            Console.WriteLine(GetMemory(str));

      Test Results:

     The test shows that once the value is modified after the assignment of the string type variable is completed, a storage space is actually re-allocated to store the modified value, and the original storage space is retained and saved. It also proves that the so-called "string type value is immutable".

  • String string concatenation performance test. String concatenation is achieved by loop simulation, and the running time is compared with the time required for stringbuilder to achieve the same function.    
      public static void StringConcat(int num)
        {
            string str = "";
            for(int i = 0; i < num; i++)
            {
                str += i.ToString();
            }
        }
        public static void StringBuilderTest(int num)
        {
            StringBuilder builder = new StringBuilder();
            for(int i = 0; i < num; i++)
            {
                builder.Append(i.ToString());
            }
            string str = builder.ToString();
        }

    Test code:

        int num = 1000;
            do
            {
                int start = Environment.TickCount;
                 /* *****Use string concatenation to build strings ***** */
                StringConcat(num);
                int middle = Environment.TickCount;

                /* *****Use StringBuilder to build strings ***** */
                StringBuilderTest(num);
                int end = Environment.TickCount;

                int t1 = middle - start;
                int t2 = end - middle;
                Console.WriteLine( " Number of cycles: {0}, StringBuilder: {1}ms, String concatenation: {2}ms " , num, t2, t1);
                num = ( int )(num * 1.5 );
            } while (num < 1000000);

    Test Results:

    

    The results show that: the performance of a large number of string concatenation is poor, which is of course determined by the immutability of the string type value, and the solution is to use stringbuilder instead.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325138900&siteId=291194637