Use StringBuilder instead of String as possible as you can.

时间:2022-11-01 21:32:54

  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:

Use StringBuilder instead of String as possible as you can.

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:

Use StringBuilder instead of String as possible as you can.

  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&num;中 StringBuilder类 与 String类的区别---(转)

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

  2. StringBuilder类与String类的区别

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

  3. &lbrack;java初探外篇&rsqb;&lowbar;&lowbar;关于StringBuilder类与String类的区别

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

  4. StringBuilder 详解 &lpar;String系列之2&rpar;

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

  5. StringBuilder&period;append&lpar;&rpar;与String的&quot&semi;&plus;&quot&semi;的效率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. 数据结构和算法 &ndash&semi; 4&period;字符串、 String 类和 StringBuilder 类

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

随机推荐

  1. Asp&period;Net Mvc4 Webapi Request获取参数

    最近用mvc4中的WEBAPI,发现接收参数不是很方便,跟传统的request.querystring和request.form有很大区别,在网上搜了一大圈,各种方案都有,但不是太详细,于是跟踪Act ...

  2. activity 所需jiar包

    activity下载后有很多jar包有的jar包不一定用得上,也有些jar是重复的,这里就整理了一下必要的jar包 路径.. \activiti-5.19.0.2\wars\activiti-expl ...

  3. 会话控制:session与cookie

    我们在浏览网站时,访问的每一个web页面都需要使用"http协议"实现.而HTTP协议是无状态协议,就是说HTTP协议没有一个内建机制来维护两个事务之间的状态.当一个用户请求一个页 ...

  4. popupwindow点击空白处如何自动消失?

    Popupwindow如果需要点击空白处自动消失,需要设置两个函数 1.customPopWindow.setFocusable(true);该函数也可以在构造函数中设置,如:mPopupWindow ...

  5. TStringList 常用操作

    //TStringList 常用方法与属性: var   List: TStringList;   i: Integer; begin   List := TStringList.Create;   ...

  6. BZOJ 1629&colon; &lbrack;Usaco2007 Demo&rsqb;Cow Acrobats

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  7. Mac OS10&period;11更新ruby,gem,安装cocoapods

    1.装cocoapods,ruby版本忒低->开始更新ruby->开始更新gem,这是一条不归路啊同志们,各种permission denied,各种路径不存在,各种路径没有读写权限,各种 ...

  8. 未能加载文件或程序集Newtonsoft&period;Json&comma; Version&equals;4&period;5&period;0&period;0

    1.打开 程序管理器控制台  输入 PM> install-package newtonsoft.json 2.查看bin文件中是否有 newtonsoft.json.dll文件 3.在Web. ...

  9. CSS层叠样式表--使用

    一.颜色属性 二.字体属性三.背景属性四.文本属性五.边框属性六.列表属性七.display属性八.内外边距九.float属性十.定位十一.margin定位 一.颜色属性 (1)英文单词 <p ...

  10. Servlet案例5:用户登录失败信息回显

    登录失败信息回显不会的新的一个页面,而是显示在登录页面 一种方法是: 登录页面表单中每个字段后添加<span>标签写入失败信息,利用ajax技术 通过改变<span>标签的di ...