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 ...
随机推荐
- windows 下 redis for php 配置
下载 redis,下载地址 https://github.com/dmajkic/redis/downloads,下载下来 zip 文件,解压,根据系统选择解压的文件夹(比如我的是 64bit). 我 ...
- 从源码角度看finish()方法的执行流程
1. finish()方法概览 首先我们来看一下finish方法的无参版本的定义: /** * Call this when your activity is done and should be c ...
- spring boot/cloud 应用监控
应用的监控功能,对于分布式系统非常重要.如果把分布式系统比作整个社会系统.那么各个服务对应社会中具体服务机构,比如银行.学校.超市等,那么监控就类似于警察局和医院,所以其重要性显而易见.这里说的,监控 ...
- gulp插件gulp-usemin简单使用
关于什么是gulp,它和grunt有什么区别等问题,这里不做任何介绍.本文主要介绍如何使用gulp-usemin这款插件,同时也会简单介绍本文中用到的一些插件. 什么是gulp-usemin 用来将H ...
- 项目分布式部署那些事(2):基于OCS(Memcached)的Session共享方案
在不久之前发布了一篇"项目分布式部署那些事(1):ONS消息队列.基于Redis的Session共享,开源共享",因为一些问题我们使用了阿里云的OCS,下面就来简单的介绍和分享下相 ...
- SSH登录之后运行命令报错的解决办法-- Failed to connect to Mir: Failed to connect to server socket: No such file or directory
问题描述: Failed to connect to Mir: Failed to connect to server socket: No such file or directory 解决方案: ...
- python基础-PyCharm设置作者信息模板_修改解释器_设置软件UTF-8编码
一.PyCharm 设置作者信息模板 1.File---Settings---在搜索框中搜索:File and Code Templates---Python scripts #!/usr/bin/e ...
- SpringMVC学习--参数绑定
spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收 ...
- canvas缓动2
同之前的缓动原理.这里将终点换成鼠标,做出跟随效果 var canvas = document.getElementById("canvas"); var cxt=canvas.g ...
- Dubbo系列(1)_背景介绍和基本情况
一.本文目的 主要介绍Dubbo的产生背景和需要解决的问题 二.产生背景 随着大数据量.高并发的互联网应用越来越多,单机系统已经无法满足系统的需要.通过SOA搭建一个分 ...