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 ...
随机推荐
- cron表达式详解(Spring定时任务配置时间间隔)
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- astyle代码格式化
Artistic Style 1.24 A Free, Fast and Small Automatic Formatterfor C, C++, C#, and Java Source Code 项 ...
- SQL server聚合函数、数学函数、字符串函数
一.基础语句 二.数学函数与字符串函数 三.练习 1.创建一个学生信息表,根据要求写出程序 2.新建一个超市表,进了十种商品,个数都是十件
- 如何测试 Android 中的定时事件
测试定时事件不太容易,比如要测试 AlarmManager 中定时明天4点的一个事件,你总不能等到明天4点再看看吧. Roman Nurik 提供了两个用来测试定时事件的命令:adb shell du ...
- cloudera安装hadoop集群和相关服务
一.软件准备: 1.下载cloudera-manager-installer.bin(安装...-server),cdh4.cm(这是...-agent),另外还有些需要的关联软件下步添加. 2.先建 ...
- 14.6.3.1 The InnoDB Buffer Pool
14.6.3.1 The InnoDB Buffer Pool InnoDB 保持一个存储区域被称为buffer pool 用于cache数据和索引在内存里, 知道InnoDB buffer pool ...
- Light OJ 1051 - Good or Bad
题目大意: 给你一个字符串,字符串由大写字母和‘?’组成,大写字母可以变成任意一个字母.现在我们定义字符串, 如果有超过三个连续的元音字母或者连续五个辅音字母,那么我们称这个字符串是“BAD”,否则称 ...
- 【转】使用命令行方式创建和启动android模拟器
原文网址:http://blog.csdn.net/tiandinilv/article/details/8953001 1.Android模拟器介绍 Android中提供了一个模拟器来模拟ARM核的 ...
- 综合(奇技淫巧):HDU 5118 GRE Words Once More!
GRE Words Once More! Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/O ...
- mac上的键盘生活——神级输入法:鼠须管
好吧,我是今天才知道这个无敌的输入法有多么强悍,传说中的神级输入法,鼠须管~ 在这之前我都一直是用的搜狗输入法,因为以前在win下就一直都用的是搜狗输入法,怎么说,各种国产的输入法做的还是比较有良心的 ...