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.的更多相关文章

  1. C#中 StringBuilder类 与 String类的区别---(转)

      在找工作的时候,去了些公司,避免不了要面试和笔试.不过一般最起初的是笔试.我印象中有这样有一道题目:StringBuilder类与 String类的区别?那时候我不太清楚这两个类的区别,今天在看代 ...

  2. StringBuilder类与String类的区别

    String对象是不可改变的,每次使用String类中的方法时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新的String对象相关的 ...

  3. [java初探外篇]__关于StringBuilder类与String类的区别

    前言 我们前面学习到String类的相关知识,知道了它是一个字符串类,并且了解到其中的一些方法,但是当时并没有太过注意到String类的特点,今天就StringBuilder类的学习来比较一下两者的区 ...

  4. StringBuilder 详解 (String系列之2)

    本章介绍StringBuilder以及它的API的详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string02.html StringB ...

  5. StringBuilder.append()与String的"+"的效率PK

    如果String通过"+"来拼接,如果拼接的字符串是常量,则效率会非常高,因为会进行编译时优化,这个时候StringBuilder的append()是达不到的. 如果将String ...

  6. String、StringBuffer和StringBuilder的深入解析

    今天闲来无事,整理了下平时记录在印象笔记里的java开发知识点,整理到String,StringBuffer以及StringBuilder的区别时突然又产生了新的疑惑,这些区别是怎么产生的?温故为何能 ...

  7. 不同Framework下StringBuilder和String的性能对比,及不同Framework性能比(附Demo)

    本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 环境搭建 测试用例 MSDN说明 ...

  8. String、StringBuffer、StringBuilder源码分析

    利用反编译具体看看"+"的过程 1 public class Test 2 { 3 public static void main(String[] args) 4 { 5 int ...

  9. 数据结构和算法 – 4.字符串、 String 类和 StringBuilder 类

    4.1.String类的应用 class String类应用 { static void Main(string[] args) { string astring = "Now is The ...

随机推荐

  1. python处理LINUX的PWD文档

    用冒号分隔的哟. 此章后面讲的JSON,配置文件读取,原理应该一样吧,只是要用合适的包去处理吧. CSV文档是用CSV包处理的. 文档: root:x:0:0:root:/root:/bin/bash ...

  2. cron表达式详解(Spring定时任务配置时间间隔)

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  3. 李洪强iOS开发之-环信01_iOS SDK 前的准备工作

    李洪强iOS开发之-环信01_iOS SDK 前的准备工作 1.1_注册环信开发者账号并创建后台应用 详细步骤:  注册并创建应用 注册环信开发者账号 第 1 步:在环信官网上点击“即时通讯云”,并点 ...

  4. struts2中获取request、response,与android客户端进行交互(文件传递给客户端)

    用struts2作为服务器框架,与android客户端进行交互需要得到request.response对象. struts2中获取request.response有两种方法. 第一种:利用Servle ...

  5. Java实现生产者消费者

    方法1:最简单--利用LinkedBlockingQueue 队列具有先进先出的特点,成为经常应用于生产-消费者模式的数据结构. 1.将一个对象放到队列尾部,如果队列已满,就等待直到有空闲节点. —— ...

  6. Android实用代码七段(一)

    前言 这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: htt ...

  7. dedecms网站如何做在线订单功能

    做网站的时候经常会遇到做在线订单的这个功能,而且这个功能会在企业网站的建设中经常的遇到,今天51模板集就拿物流网的在线订单功能做一个详细的介绍. 第一步:自定义表单 打开后台:核心-->自定义表 ...

  8. Linux学习笔记4——函数调用栈空间的分配与释放

    一.函数执行时使用栈空间作为自己的临时栈,3种方式决定编译器清空栈的方式:__stdcall. __fastcall.__cdecl 1.__stdcall表示每个调用者负责清空自己调用的函数的临时栈 ...

  9. centos6.5建立cloudera-cdh4.6本地源

    1.准备:      centos6.5系统,root用户 2.安装所需包:      sudo yum install yum-utils createrepo 3.下载cdh4.6的repo:   ...

  10. 《A First Course in Probability》-chaper2-概率论公理

    概率论自身有一套很深的理论体系,读过<几何原本>的读者会知道,伟大的欧几里得之所以伟大,是因为它基于几条最基本的公理,推导除了整个欧式几何学的理论体系,同样,在概率论这里,一切的推导都是源 ...