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. SolrCloud阶段总结

    http://www.cnblogs.com/guozk/p/3498844.html SolrCloud阶段总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍SolrClou ...

  2. python装饰器之使用情景分析

    http://blog.csdn.net/yueguanghaidao/article/details/10089181

  3. xml中1字节的UTF-8序列的字节1无效([字符编码]Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案)

    今天在eclipse中编写pom.xml文件时,注释中的中文被eclipse识别到错误:Invalid byte 1 of 1-byte UTF-8 sequence,曾多次遇到该问题,问题的根源是: ...

  4. Spark如何解决常见的Top N问题

    需求   假设我们有一张各个产品线URL的访问记录表,该表仅仅有两个字段:product.url,我们需要统计各个产品线下访问次数前10的URL是哪些?   解决方案   (1)模拟访问记录数据   ...

  5. Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍

    磁盘分区表.文件系统的查看.统计的工具很多,有些工具是多功能的,不仅仅是查看磁盘的分区表,而且也能进行磁盘分区的操作:但在本文,我们只讲磁盘分区的查看,以及分区的使用情况的查看:本文只是给新手上路之用 ...

  6. godaddy.com 注册域名 买卖域名

    https://www.godaddy.com/domains/searchresults.aspx?ci=83269&checkAvail=1&domainToCheck=ses.x ...

  7. UVA 10494 (13.08.02)

    点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...

  8. intellij安装 配置 创建项目

    使用intellij创建项目的整个过程如下: 首先,点击intllij的.exe文件,如果是第一次安装,选择第二个选项即可 Intellij需要license key,可以使用注册机生成相应的name ...

  9. C基础

    一.关于整型数据 1.整型常量:十进制数前面可以加+.-号,但是不能有前缀0 八进制数:必须以前缀0开头,不是O.不能加负号(-),否则不能识别. 十六进制数:前缀必须为0x或者0X.不能加负号(-) ...

  10. Python自带的日志模块logging的使用

    import logging     # 创建一个logger logger = logging.getLogger('cmccLogger') logger.setLevel(logging.DEB ...