String 与StringBuffer比较
2016-06-11 14:01
239 查看
package String比较; /* * String 与StringBuffer比较 * String 不可变,一旦赋值,就不能被修改 * StringBuffer可变的字符串。 * StringBuffer的追加效率更高 */ public class Test8 { public static void main(String[] args) { // TODO Auto-generated method stub String str = "abc"; str.replace("a", "b"); System.out.println(str);// abc StringBuffer sbf = new StringBuffer("abc"); sbf.replace(0, 1, "de"); System.out.println(sbf);// debc String s1 = "chinasofti"; int time = 10000; @SuppressWarnings("unused") String tempstr = ""; long start1 = System.currentTimeMillis(); for (int i = 0; i < time; i++) { tempstr += s1; } long end1 = System.currentTimeMillis(); System.out.println("String:" + (end1 - start1) + "ms");// 876ms StringBuffer tempsbf = new StringBuffer(); long start2 = System.currentTimeMillis(); for (int i = 0; i < time; i++) { tempsbf.append(s1); } long end2 = System.currentTimeMillis(); System.out.println("StringBuffer:" + (end2 - start2) + "ms");// 1ms } }
相关文章推荐
- node安装相关
- leetcode 117 Populating Next Right Pointers in Each Node II
- css 动画 transform transition animation
- H5选择器之大放异彩 仿jquery链式调用 IE8+完美运行
- 谈谈JsBridge实现
- CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]
- Remove Duplicates from Sorted List
- js获取客户端IP及地理位置
- JavaScript的数据类型和变量声明以及js的语句
- leetcode 116 Populating Next Right Pointers in Each Node
- jquery动画之slide
- 菜鸟碎碎念之JS+Echarts
- 剑指offer(五十八)之正则表达式匹配
- 【CSS笔记六】CSS盒模型
- JavaScript手机振动API
- jquery.cookie用法
- JavaScript地理位置信息API
- React.js学习之理解JSX和组件
- 剑指offer(17)-包含min函数的栈
- JS去重方法