stand on the shoulders of giants

对StringBuilder的Dump分析

转载自http://www.cnblogs.com/juqiang
对于字符串相加,建议使用StringBuilder,而不是普通的string concat,为什么呢?

代码

代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace SOSBasics2
{
    
class Program
    {
        
static void Main(string[] args)
        {
            StringTest st 
= new StringTest();
            
long t1 = st.StringConcat("hello"10000);
            
long t2 = st.StringBuilder("world"10000);

            Console.WriteLine(
"String concat cost: {0:N0} ticks", t1);
            Console.WriteLine(
"String builder cost: {0:N0} ticks", t2);

            Console.WriteLine(
"=========================================================");
            Console.WriteLine(
"到命令行下面,然后切换到windbg目录,执行adplus -hang -pn cosoleTry.exe -o c:\\dumps");
            Console.ReadLine();
        }
    }

    
public class StringTest
    {
        
public long StringConcat(string input, int repeat)
        {
            Stopwatch sw 
= new Stopwatch();
            sw.Start();
            
string s = "";

            
for (int i = 0; i < repeat;i++)
            {
                s 
+= input;
            }

            sw.Stop();

            
return sw.Elapsed.Ticks;
        }

        
public long StringBuilder(string input, int repeat)
        {
            Stopwatch sw 
= new Stopwatch();
            sw.Start();
            StringBuilder sb 
= new StringBuilder();

            
for (int i = 0;i < repeat;i++)
            {
                sb.Append(input);
            }

            
string s = sb.ToString();
            sw.Stop();

            
return sw.Elapsed.Ticks;
        }
    }
}

执行结果:
String concat cost: 6,837,727 ticks
String builder cost: 7,216 ticks
=========================================================

明显可以看出StringBuilder在时间上的优势

接下来分析Dump文件:
!dumpheap -stat
...................................................
61f42b18      264         6336 System.Collections.ArrayList
61f142b8      305        36548 System.Object[]
004925f0       31      1060712      Free
61f40ae8      584      2315936 System.String
Total 1861 objects

0:000> !dumpheap -mt 61f40ae8 -strings (strings这个参数的意思是,只输出字符串,其他的都省略掉)
total 576 objects
Statistics:
Count   TotalSize   String Value
.................................................
10       262092 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20      1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"

可以看出来StringBuilder在空间上的优势也十分明显。

如果把代码更改一下StringBuilder sb = new StringBuilder(64<<10); (64<<10 相当于64×1024)
预先分配好64K的内存,运行结果可以看出StringBuilder更加优化。
String concat cost: 7,045,500
String builder cost: 5,798
=========================================================

分析Dump发现,
1       131096 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20      1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
预先分配好足够内存,可以避免频繁分配内存,这个例子来看,字符串只分配了一次,而上面分配了10次

测试:
string s = "hello";
s += "world";
s += "new";
s += "peace";

然后你分析Dump,查看字符串,会发现存在4个对象:
1、hello
2、helloworld
3、helloworldnew
4、helloworldnewpeace

posted @ 2010-09-16 17:35  DylanWind  阅读(450)  评论(0编辑  收藏  举报