测试代码:

public class StringJoinTest {
public static void main(String[] args) {
int count = 10000;
long begin, end, time;
begin = System.currentTimeMillis();
testString(count);
end = System.currentTimeMillis();
time = end - begin;
System.out.println("拼接" + count + "次,String消耗时间:" + time + "毫秒"); begin = System.currentTimeMillis();
testStringBuffer(count);
end = System.currentTimeMillis();
time = end - begin;
System.out.println("拼接" + count + "次,StringBuffer消耗时间:" + time + "毫秒"); begin = System.currentTimeMillis();
testStringBuilder(count);
end = System.currentTimeMillis();
time = end - begin;
System.out.println("拼接" + count + "次,StringBuilder消耗时间:" + time + "毫秒");
} private static String testStringBuilder(int count) {
StringBuilder tem = new StringBuilder();
for (int i = 0; i < count; i++) {
tem.append("hello world!");
}
return tem.toString();
} private static String testStringBuffer(int count) {
StringBuffer tem = new StringBuffer();
for (int i = 0; i < count; i++) {
tem.append("hello world!");
}
return tem.toString();
} private static String testString(int count) {
String tem = "";
for (int i = 0; i < count; i++) {
tem += "hello world!";
}
return tem;
}
}

测试结果:

结论:

  在少量字符串拼接时还看不出差别,但随着数量的增加,String+拼接效率显著降低。在达到100万次,我本机电脑已经无法执行String+拼接了,StringBuilder效率略高于StringBuffer。所以在开发过程中通常情况下推荐使用StringBuilder。

  StringBuffer和StringBuilder的区别在于StringBuffer是线程安全的。

Java字符串拼接效率测试的更多相关文章

  1. Java字符串拼接效率对比

    1.来自:http://blog.csdn.net/Zen99T/article/details/51255418 2.来自:http://blog.csdn.net/kimsoft/article/ ...

  2. Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740

    Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...

  3. Java 字符串拼接 五种方法的性能比较分析 从执行100次到90万次

    [请尊重原创版权,如需引用,请注明来源及地址] > 字符串拼接一般使用“+”,但是“+”不能满足大批量数据的处理,Java中有以下五种方法处理字符串拼接,各有优缺点,程序开发应选择合适的方法实现 ...

  4. 羞,Java 字符串拼接竟然有这么多姿势

    二哥,我今年大二,看你分享的<阿里巴巴 Java 开发手册>上有一段内容说:"循环体内,拼接字符串最好使用 StringBuilder 的 append 方法,而不是 + 号操作 ...

  5. Java 字符串拼接四种方式的性能比较分析

    一.简单介绍 编写代码过程中,使用"+"和"contact"比较普遍,但是它们都不能满足大数据量的处理,一般情况下有一下四种方法处理字符串拼接,如下: 1. 加 ...

  6. Java 字符串拼接 StringBuilder() StringBuffer

            字符串拼接 普通方式 public class StringDemo2 { public static void main(String[] args) { // 表示获取从1970- ...

  7. JAVA字符串拼接操作规则说明

    1.常量与常量的拼接结果在常量池,原理是编译期优化 public void test1() { String s1 = "a" + "b" + "c& ...

  8. java字符串拼接的几种方式

    1. plus方式 当左右两个量其中有一个为String类型时,用plus方式可将两个量转成字符串并拼接. String a="";int b=0xb;String c=a+b;2 ...

  9. list 字符串拼接效率实验

    ist 字符串拼接有多种方法,我就其中常用三种做了实验,实验代码如下: 第一次是为了初始化静态方法,后面的才是效率比较. 结果如下: StringUtils join 方法用的是StringBuild ...

随机推荐

  1. STL_string.vector中find到的iterator的序号

    ZC:注意,printf("0x%08X\n",vtr.end()); 打印出来 应该就是 0x00000000,∵ 它就是 指向最后一个元素的后面,应该是理解成 无意义      ...

  2. [转][osg][QT]osg与QT界面结合的简单例子

    //QT += core gui opengl //LIBS += -losgViewer -losgDB -losgUtil -losg -lOpenThreads -losgGA -losgQt ...

  3. adb shell命令后出现error: device not found错误提示

    在cmd中输入adb shell进入linux shell环境前,需要把android模拟器打开(本文都是针对模拟器而言,并非真机).如果启动好了模拟器,且输入adb shell命令后出现error: ...

  4. vscode下ts-node传入cli参数

    ts-node写ts,启动时习惯在package.json里写 "scripts": { "build-ts": "tsc", " ...

  5. SQL service 中的 ”输入SQL命令窗口“ 打开了 “属性界面” 回到 ”输入SQL命令窗口“

    输入SQL命令窗口点击上面的菜单栏中的 “窗口”

  6. angular 拦截器

    介绍:$http service在Angular中用于简化与后台的交互过程,其本质上使用XMLHttpRequest或JSONP进行与后台的数据交互.在与后台的交互过程中,可能会对每条请求发送到Ser ...

  7. Python3 操作Excel

    首先说明一下 在处理大文件时,openpyxl 的性能不如 xlrd,xlwt等.所以可以读取的时候使用xlrd,写的时候用openpyxl. 今天遇到一个使用场景:excel存放的是一条条用例,包含 ...

  8. Django - Python3 配置 MySQL

    在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装 具体安装使用方法,可参考 Python3 - MySQL适配器 PyMySQL Django 如何链接 MySQL 数据库, 需要在 ...

  9. Server SQL Modes

    The MySQL server can operate in different SQL modes, and can apply these modes differently for diffe ...

  10. delphi 程 序从exe运行改成dll库

    第一种方法: 具体步骤: 1.生成新的或着打开已经存在的工程文件(DPR)(Project1.dpr). 2.选择[View]—>[Project Manager],选中[ProjectGrou ...