程序人生
博客园
社区
首页
新随笔
联系
管理
订阅
随笔- 20 文章- 0 评论- 184
Basic Windbg - 1. SOSBasics(总结)(转)
原文地址:
http://www.cnblogs.com/juqiang/archive/2008/01/02/1023291.html
Basic Windbg - 1. SOSBasics(总结)
我们都知道,对于字符串相加,建议使用StringBuilder,而不是普通的string concat,为什么呢?我们通过dump简单看一下。
先看这个代码:
Code
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
SOSBasics2
6
{
7
class
Program
8
{
9
static
void
Main(
string
[] args)
10
{
11
StringTest st
=
new
StringTest();
12
long
t1
=
st.StringConcat(
"
hello
"
,
10000
);
13
long
t2
=
st.StringBuilder(
"
world
"
,
10000
);
14
15
Console.WriteLine(
"
String concat cost: {0:N0}
"
, t1);
16
Console.WriteLine(
"
String builder cost: {0:N0}
"
, t2);
17
18
Console.WriteLine(
"
=========================================================
"
);
19
Console.WriteLine(
"
到命令行下面,然后切换到windbg目录,执行adplus -hang -pn sosbasics2.exe -o c:\\dumps
"
);
20
Console.ReadLine();
21
}
22
}
23
24
public
class
StringTest
25
{
26
public
long
StringConcat(
string
input,
int
repeat)
27
{
28
long
tick1
=
DateTime.Now.Ticks;
29
string
s
=
""
;
30
31
for
(
int
i
=
0
; i
<
repeat;i
++
)
32
{
33
s
+=
input;
34
}
35
36
long
tick2
=
DateTime.Now.Ticks;
37
38
return
tick2
-
tick1;
39
}
40
41
public
long
StringBuilder(
string
input,
int
repeat)
42
{
43
long
tick1
=
DateTime.Now.Ticks;
44
StringBuilder sb
=
new
StringBuilder();
45
46
for
(
int
i
=
0
;i
<
repeat;i
++
)
47
{
48
sb.Append(input);
49
}
50
51
string
s
=
sb.ToString();
52
long
tick2
=
DateTime.Now.Ticks;
53
54
return
tick2
-
tick1;
55
}
56
}
57
}
58
对于hello字符串,我们加10000次,然后得到时间差。对于world字符串,我们也加10000次,然后得到时间差。按照前面讲的抓dump的方法,我们得到dump,然后看!dumpheap -stat的结果:
0:000> !dumpheap -stat
total 3686 objects
Statistics:
MT Count TotalSize Class Name
791334a8 1 12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]]
79119954 1 12 System.Security.Permissions.ReflectionPermission
79119834 1 12 System.Security.Permissions.FileDialogPermission
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
79104368 266 6384 System.Collections.ArrayList
7912d8f8 299 31408 System.Object[]
004ad640 31 1060716 Free
790fd8c4 2399 2423452 System.String
Total 3686 objects
然后我们看字符串的东西:!dumpheap -mt 790fd8c4 -strings(strings这个参数的意思是,只输出字符串,其他的都省略掉),结果如下:
0:000> !dumpheap -mt 790fd8c4 -strings
total 2399 objects
Statistics:
Count TotalSize String Value
1 20 "}"
1 20 "{"
1 20 "x"
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
2 5600 "<PermissionSet class="System.Security.NamedPermissionSet"versio"
1 9280 "<NamedPermissionSets><PermissionSet class="System.Security.Name"
10 262092 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20 1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2399 objects
值得我们注意的是,world字符串(一大堆),一共有10个,大小是262092,hello字符串一共有20个,大小是1998520。两者为啥相差这么大?书上说,string concat会不停的产生临时对象,这些对象都会占用内存的,so,会比较慢,也比较吃内存。而StringBuilder是预先分配好了一块内存来用,so,会快一些。从上面的结果来看,貌似是这样的。
好,那我们修改一下代码吧!
Code
1
public
long
StringBuilder(
string
input,
int
repeat)
2
{
3
long
tick1
=
DateTime.Now.Ticks;
4
StringBuilder sb
=
new
StringBuilder(
64<<10
);
5
6
for
(
int
i
=
0
;i
<
repeat;i
++
)
7
{
8
sb.Append(input);
9
}
10
11
string
s
=
sb.ToString();
12
long
tick2
=
DateTime.Now.Ticks;
13
14
return
tick2
-
tick1;
15
}
16
我们对StringBuilder的参数做点手脚,让它一次性的初始化64K大小,重新抓dump,然后重新用!dumpheap -mt和!dumpheap -stat来看。
1 131096 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20 1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2387 objects
这个结果比较有意思吧!如果我们让stringBuilder的buffer足够大,那么它可以避免频繁分配内存。这个例子来看,字符串只分配了两次而已(上面还有一个5个字节的,我没有列出)
posted @ 2008-03-11 17:18
鹰击长空
阅读(34)
评论(0)
编辑
收藏
所属分类:
WinDbg
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
另存
打印
所属分类的其他文章:
·
Basic Windbg - 2 CLR基础 (转)
·
Basic Windbg - 1. SOSBasics(总结)(转)
·
Basic Windbg - 1. SOSBasics(再续)(转)
·
Basic Windbg - 1. SOSBasics(续)(转)
·
Basic Windbg - 1. SOSBasics(转)
最新IT新闻:
·
奥运核心资源被分食 搜狐央视网谁忽悠谁?
·
微软推新型搜索技术"BrowseRank"挑战谷歌
·
2008年7月26日IT博客精选
·
微软每年向Apache捐10万美元支持开源软件
·
AOL将关闭3个网站以降低成本 集中发力广告
博客园新闻频道
博客园首页
社区
公告
<
2008年3月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
与我联系
发短消息
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我参加的小组
Ext 2.0
WinForms
宁波.net俱乐部
我参与的团队
Design & Pattern团队(0/813)
宁波.NET俱乐部(0/56)
Debug 探索团队(0/46)
开源CLI核心探索团队(0/82)
WCF技术研究团队(0/122)
经验汇(0/26)
我的标签
设计模式(8)
datagridview(2)
列合并(1)
既有文本框又有按钮(1)
sql(1)
数据库(1)
代码管理器(1)
随笔分类
WinDbg(5)
(rss)
WinForm(2)
(rss)
代码管理器(1)
(rss)
设计模式(9)
(rss)
数据库设计(1)
(rss)
随笔档案
2008年6月 (1)
2008年5月 (3)
2008年4月 (4)
2008年3月 (10)
文章分类
数据库(1)
(rss)
收藏夹
DataTable(1)
(rss)
Remoting(1)
(rss)
程序员资源(8)
(rss)
对面向对象的一些理解(1)
(rss)
模板设计(1)
(rss)
个人收藏
搜索
积分与排名
积分 - 29815
排名 - 1301
最新评论
1. re: 打造属于自己的代码管理器之需求分析篇
谢谢大家支持,不过最近公司项目紧,没时间做。 (鹰击长空)
2. re: 打造属于自己的代码管理器之需求分析篇
支持,早就想自己做一个了,这种工具比较少见啊
加油! (daoyuly)
3. re: 《Head.First设计模式》的学习笔记(7)--命令模式
很不错 (秋色)
4. re: 打造属于自己的代码管理器之需求分析篇
各位为什么不加设一台subversion服务器,然后将你的代码提交上去呢,服务器就是你的代码库,而tortoriseSVN就是你访问代码的代码获取工具 [不过subversion没有提供查询功能,是不... (v0860)
5. re: 打造属于自己的代码管理器之需求分析篇
加你在宁波.net俱乐部了 (董昊(昊子))
阅读排行榜
1. SQL语句优化技术分析(3429)
2. 《Head.First设计模式》的学习笔记(2)--策略模式(2891)
3. 《Head.First设计模式》的学习笔记(4)--装饰者模式(2333)
4. 《Head.First设计模式》的学习笔记(5)--工厂方法模式(2190)
5. 《Head.First设计模式》的学习笔记(6)--单件模式(2019)
评论排行榜
1. SQL语句优化技术分析(28)
2. 打造属于自己的代码管理器之需求分析篇(24)
3. 《Head.First设计模式》的学习笔记(6)--单件模式(19)
4. 《Head.First设计模式》的学习笔记(2)--策略模式(19)
5. 《Head.First设计模式》的学习笔记(4)--装饰者模式(18)