使用

  • Concatenation Operator (+)
  • String concat method – concat(String str)
  • StringBuffer append method – append(String str)
  • StringBuilder append method – append(String str)

进行性能测试。

环境 win7 32位, cpu双核2.5GHZ,2G内存。

测试代码如下:

    private final static int OUTER_ITERATION = 10;
private final static int INNER_ITERATION = 50000; /**
* @param args
*/
public static void main(String[] args) {
String addTestStr = "";
String concatTestStr = "";
StringBuffer concatTestSb = null;
StringBuilder concatTestSbu = null; for (int outerIndex = 0; outerIndex < OUTER_ITERATION; outerIndex++) {
StopWatch stopWatch = new LoggingStopWatch("StringAddConcat");
addTestStr = "";
for (int innerIndex = 0; innerIndex < INNER_ITERATION; innerIndex++)
addTestStr += "*";
stopWatch.stop();
} for (int outerIndex = 0; outerIndex < OUTER_ITERATION; outerIndex++) {
StopWatch stopWatch = new LoggingStopWatch("StringConcat");
concatTestStr = "";
for (int innerIndex = 0; innerIndex < INNER_ITERATION; innerIndex++)
concatTestStr.concat("*");
stopWatch.stop();
} for (int outerIndex = 0; outerIndex < OUTER_ITERATION; outerIndex++) {
StopWatch stopWatch = new LoggingStopWatch("StringBufferConcat");
concatTestSb = new StringBuffer();
for (int innerIndex = 0; innerIndex < INNER_ITERATION; innerIndex++)
concatTestSb.append("*");
stopWatch.stop();
} for (int outerIndex = 0; outerIndex < OUTER_ITERATION; outerIndex++) {
StopWatch stopWatch = new LoggingStopWatch("StringBuilderConcat");
concatTestSbu = new StringBuilder();
for (int innerIndex = 0; innerIndex < INNER_ITERATION; innerIndex++)
concatTestSbu.append("*");
stopWatch.stop();
} }

测试结果:
Performance Statistics   2010-04-08 06:16:00 - 2010-04-08 06:16:30

Tag Avg(ms) Min Max Std Dev Count
StringAddConcat 9355.4 7860 10046 547.7 10
StringBufferConcat 3.5 0 5 2.3 10
StringBuilderConcat 2.0 0 5 2.4 10
StringConcat 3.1 0 6 2.5 10

java字符串拼接与性能的更多相关文章

  1. 【转】Java 5种字符串拼接方式性能比较。

    最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...

  2. Java 5种字符串拼接方式性能比较。

    最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...

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

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

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

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

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

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

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

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

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

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

  8. java 字符串拼接

    package com.fh.controller.pacm.checkbill; import com.google.common.base.Joiner; /** * 字符串拼接 * * @aut ...

  9. Java 5种字符串拼接方式性能比较

    http://blog.csdn.net/kimsoft/article/details/3353849 import java.util.ArrayList; import java.util.Li ...

随机推荐

  1. 从大公司做.NET 开发跳槽后来到小公司的做.NET移动端微信开发的个人感慨

    从14年11月的实习到正式的工作的工作我在上一家公司工作一年多了.然而到16年5月20跳槽后自己已经好久都没有在写博客了,在加上回学校毕业答辩3天以及拿档案中途耽搁了几天的时间,跳槽后虽然每天都在不停 ...

  2. GitHub中国区前100名到底是什么样的人?

    本文根据Github公开API,抓取了地址显示China的用户,根据粉丝关注做了一个排名,分析前一百名的用户属性,剖析这些活跃在技术社区的牛人到底是何许人也!后续会根据我的一些经验出品<技术人员 ...

  3. C#链接阿里云KVStore

    KVStore的简单介绍 阿里云KVStore兼容Redis.因为KVStore就相当于Redis的服务器端,我们代码只是当作客户端,链接上服务器端就行了,阿里云的KVStore详情文档见,https ...

  4. socket.io简单说明及在线抽奖demo

    socket.io简单说明及在线抽奖demo socket.io 简介 Socket.IO可以实现实时双向的基于事件的通信. 它适用于各种平台,浏览器或设备,也同样注重可靠性和速度. socket.i ...

  5. 使用Python 将shapefile导入mongodb

    使用Python 将shapefile导入mongodb 随着big data时代的到来,各个行业都在考虑能不能把big data的思路.方法引入进来,GIS行业也不能免俗. 下面就介绍一下如何将sh ...

  6. SQL Server使用游标或临时表遍历数据

    方法一:使用游标(此方法适用所有情况,对标结构没有特殊要求.) declare @ProductName nvarchar() declare pcurr cursor for select Prod ...

  7. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  8. C++折半插入排序

    代码如下: #include <iostream> using namespace std; void insertSort(int a[], int n) { for(int i=1;i ...

  9. Msyql-检测数据库版本

    show variables like '%version%'; 数据库版本结果: "protocol_version","" "version&qu ...

  10. awk打开多个文件的方法

    1.当awk读取的文件只有两个的时候,比较常用的有三种方法(1)awk 'NR==FNR{...}NR>FNR{...}' file1 file2 (2)awk 'NR==FNR{...}NR! ...