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 ...
随机推荐
- 2016年 IT 趋势大预测!
新年伊始,有不少人在总结过去,也有一些人在展望未来.下面让我们跟随 OpsClarity 的 Dhruv Jain,看看他对 2016 IT 趋势有什么大胆的预测. 又到了众人纷纷对下一年进行预测的时 ...
- Ruby安装和简介
Ruby下载地址:https://www.ruby-lang.org/zh_cn/downloads/ 我安装的是RubyInstaller.it is a self-contained Window ...
- wcf 请考虑增加操作超时
发送到 net.tcp://192.168.1.18:8732/CallbackService 的请求操作在配置的超时(00:00:59.9879994)内未收到回复. 分配给此操作的时间可能已经是更 ...
- HDU-4570 Multi-bit Trie
http://acm.hdu.edu.cn/showproblem.php?pid=4570 Multi-bit Trie Time Limit: 2000/1000 MS (Java/Others) ...
- StringBuilder字符串缓冲区
JDK1.5出现StringBuiler:构造一个其中不带字符的字符串生成器,初始容量为 16 个字符.该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候( ...
- HDU 4716 A Computer Graphics Problem 2013年四川省赛题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 题目大意不用解释了吧,看案例就能明白 #include<cstdio> #inclu ...
- typedef用法小结
typedef用法小结- - 注意:本文转自网络,版权归原作者所有. typedef typedef用法小结- - 这两天在看程序的时候,发现很多地方都用到typedef,在结构体定义,还有一些数组等 ...
- Sicily1151:魔板搜索及优化
最终优化代码地址: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1151.c 题目如下 Constraints ...
- 《A First Course in Probability》-chaper3-条件概率和独立性-基本公式
EX1: 乔伊80%肯定他把失踪的钥匙放在了他外套两个口袋中的一个.他40%确定放在左口袋,40%确定放在右口袋.如果检查了左口袋发现没有找到钥匙,那么钥匙在右口袋的条件概率是多少? 分析:很基本的条 ...
- openStack 瓶颈测试