java字符串拼接与性能
使用
- 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字符串拼接与性能的更多相关文章
- 【转】Java 5种字符串拼接方式性能比较。
最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...
- Java 5种字符串拼接方式性能比较。
最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...
- Java 字符串拼接 五种方法的性能比较分析 从执行100次到90万次
[请尊重原创版权,如需引用,请注明来源及地址] > 字符串拼接一般使用“+”,但是“+”不能满足大批量数据的处理,Java中有以下五种方法处理字符串拼接,各有优缺点,程序开发应选择合适的方法实现 ...
- Java 字符串拼接四种方式的性能比较分析
一.简单介绍 编写代码过程中,使用"+"和"contact"比较普遍,但是它们都不能满足大数据量的处理,一般情况下有一下四种方法处理字符串拼接,如下: 1. 加 ...
- Java 字符串拼接 StringBuilder() StringBuffer
字符串拼接 普通方式 public class StringDemo2 { public static void main(String[] args) { // 表示获取从1970- ...
- 羞,Java 字符串拼接竟然有这么多姿势
二哥,我今年大二,看你分享的<阿里巴巴 Java 开发手册>上有一段内容说:"循环体内,拼接字符串最好使用 StringBuilder 的 append 方法,而不是 + 号操作 ...
- JAVA字符串拼接操作规则说明
1.常量与常量的拼接结果在常量池,原理是编译期优化 public void test1() { String s1 = "a" + "b" + "c& ...
- java 字符串拼接
package com.fh.controller.pacm.checkbill; import com.google.common.base.Joiner; /** * 字符串拼接 * * @aut ...
- Java 5种字符串拼接方式性能比较
http://blog.csdn.net/kimsoft/article/details/3353849 import java.util.ArrayList; import java.util.Li ...
随机推荐
- 仿各种APP将文章DOM转JSON并在APP中以列表显示(android、ios、php已开源)
背景 一直以来都想实现类似新闻客户端.鲜城等文章型app的正文显示,即在web editor下编辑后存为json,在app中解析json并显示正文. 网上搜过,没找到轮子.都是给的思路,然后告知是公司 ...
- eclipse汉化全程
在开始之前我说一下我的环境,eclipse版本eclipse-java-indigo-SR2-win32-x86_64,操作系统Win7,但是这个基本上没有影响.红字的那个注意一下,在下面需要根据这个 ...
- SDRAM基础知识
SDRAM知识普及 在学习SDRAM之前,必须先了解"SDRAM"这个概念性的东西,并有感性的认识转变到一种理性的认识,所谓理性的认识就是实质性的东西…….不多说,相信你已经迫不急 ...
- 【对noip结束后一个月内的总结】
最近在刷一些树结构,但发现没有一个提纲,觉得有点不知所措,经常学完一个就发现还有比它更好的,而且比较耗时间.于是沙茶准备按顺序刷bzoj的省选题,看看效果怎么样……求大神指教
- Uwp Windows10获取设备位置(经纬度)
先在Package.appxmanifest中配置位置权限 2. 创建LocationManager类 using System; using System.Collections.Generic; ...
- RHCSA试题分享
RHCSA 部分 密码破解 在linux16最后加入 rd.break 进入交换模式以后输入下列指令: mount -o remount,rw /sysroot chroot /sysroot pa ...
- [转]xml文件中的转义字符
原文地址:http://www.cnblogs.com/zhxhdean/archive/2012/02/08/2342498.html 如果在XML文档中使用类似"<" 的 ...
- jQuery报 SyntaxError: expected expression, got '<'错误
这有什么可奇怪的,这个问题是表达式未能按照预期结束,说白了就是你少写分号了. 你肯定是语法错了,仔细查看一下提示错误的那一行和它的附近,是不是因为疏忽大意出错了. 再给你的建议,不要觉得某个分号可以省 ...
- Spring 配置文件中将common.properties文件外置
将配置文件的路径从项目中移出来 1. 在springApplicationContext中 <context:property-placeholder location="file:$ ...
- android studio-创建第一个项目
打开android studio 开始界面和Xcode有点类似,点击New project新建一个工程,新建过程和在Eclipse上差不多,这里就不赘述了. 下面开始新建项目 填写项目名称,和存放地址 ...