Use StringBuilder instead of String as possible as you can.
If you are care a littile about the time your algorithm cost,you should notice that,you my use StringBuilder instead of string itself if you gonna change the string literals.
Today,I test them,and the result is so much difference.
Nomal string operates:
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
string noBu = "";
for (int i = ; i < ; i ++ ) {
noBu += i;
}
sw.Stop();
MessageBox.Show(sw.Elapsed.ToString());
And the result is show with the screenshot below:

Do the same work using StringBuilder instead:
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
StringBuilder bu = new StringBuilder();
for (int i = ; i < ; i ++ ) {
bu.Append(i);
}
sw.Stop();
MessageBox.Show(sw.Elapsed.ToString());
And shows the result with the screenshot below:

Now back to our issue,if we do our job regardless the time costs(thought it is impossible),and just focus on the memory it cost.We will easy to know that,if we do use the normal string,for instance,doing
"a" + "b"
expression, it needs to create a memory for "a" literal and one for "b" literal,and create one for "ab"(the string plus result).
Whereas,if we use StringBuilder instead,since it is not fixed.
So it is obvious that the Time Complexity and the Space Complexity between them now.
Use StringBuilder instead of String as possible as you can.的更多相关文章
- C#中 StringBuilder类 与 String类的区别---(转)
在找工作的时候,去了些公司,避免不了要面试和笔试.不过一般最起初的是笔试.我印象中有这样有一道题目:StringBuilder类与 String类的区别?那时候我不太清楚这两个类的区别,今天在看代 ...
- StringBuilder类与String类的区别
String对象是不可改变的,每次使用String类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新的String对象相关的 ...
- [java初探外篇]__关于StringBuilder类与String类的区别
前言 我们前面学习到String类的相关知识,知道了它是一个字符串类,并且了解到其中的一些方法,但是当时并没有太过注意到String类的特点,今天就StringBuilder类的学习来比较一下两者的区 ...
- StringBuilder 详解 (String系列之2)
本章介绍StringBuilder以及它的API的详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string02.html StringB ...
- StringBuilder.append()与String的"+"的效率PK
如果String通过"+"来拼接,如果拼接的字符串是常量,则效率会非常高,因为会进行编译时优化,这个时候StringBuilder的append()是达不到的. 如果将String ...
- String、StringBuffer和StringBuilder的深入解析
今天闲来无事,整理了下平时记录在印象笔记里的java开发知识点,整理到String,StringBuffer以及StringBuilder的区别时突然又产生了新的疑惑,这些区别是怎么产生的?温故为何能 ...
- 不同Framework下StringBuilder和String的性能对比,及不同Framework性能比(附Demo)
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 环境搭建 测试用例 MSDN说明 ...
- String、StringBuffer、StringBuilder源码分析
利用反编译具体看看"+"的过程 1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 int ...
- 数据结构和算法 – 4.字符串、 String 类和 StringBuilder 类
4.1.String类的应用 class String类应用 { static void Main(string[] args) { string astring = "Now is The ...
随机推荐
- Caocao's Bridges
hdu4738:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:抽象出来就是求一条边权最小的割边. 题解:直接用tarjan即可破.但是如果只注重这 ...
- 【POJ1470】Closest Common Ancestors
Description Write a program that takes as input a rooted tree and a list of pairs of vertices. For e ...
- QT变异版本下载(SJLJ长跳转,DWARF不传递错误(32位专用),SEH(64位专用)),以及QT的实验室项目
http://www.tver-soft.org/ http://sourceforge.net/projects/qt64ng/ ---------------------------------- ...
- SQL 能做什么?
SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记录 SQL 可更新数据库中的数据 SQL 可从数据库删除记录 SQL 可创建新数据库 SQL ...
- Unity3d shader之次表面散射(Subsurface Scattering)
次表面散射是一种非常常用的效果,可以用在很多材质上如皮肤,牛奶,奶油奶酪,番茄酱,土豆等等 初衷是想做一个牛奶shader的,但后来就干脆研究了sss这是在vray上的次表面散射效果 这是本文在un ...
- 常见sql的error解决方法
1.sqlserver 2008 login failed: error 18456 这是很可能是因为你没有选择sql and windows登录的模式导致sql server的用户登录失败 解决方法 ...
- Hash(4) hashtable,hashmap
首先,我们要知道set是利使用map是实现的,因为只要利用map中的key唯一性就行了. 1.hashmap 和hashtable的区别是什么? 我们可以背出: hashtable线程安全.hash ...
- Sicily1151:魔板搜索及优化
最终优化代码地址: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1151.c 题目如下 Constraints ...
- 3D FPS游戏中Player摄像机的运动
最近遇到一些类似3D FPS的game play.针对这里面Player摄像机的运动的问题,随便总结一些东西以备参考.首先,对问题做一些的假设: Player摄像机在多数情况下在场景中的位置是固定的, ...
- galera mysql 多主复制启动顺序及命令
Mysql 被复制机 sql启动