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 ...
随机推荐
- Bootstrap系列 -- 9. 表格
一. Bootstrap 表格样式支持 Bootstrap提供了六种不同风格的样式支持,其中一个基础样式,4个附件样式,1个响应式设计样式 1. .table:基础表格 2. .table-strip ...
- Sentinel-Redis高可用方案(一):主从复制
引言 大概是因为Redis是个人开发的产品,所以Redis的高可用方案是被分成了几块来实现:主从复制.主从切换以及虚拟IP或客户端方案. 从Redis 2.8开始加入对Sentinel机制从而实现了服 ...
- SQL Server output子句用法 output inserted.id 获取刚插入数据的id
--插入数据,并返回刚刚插入的数据id INSERT INTO [soloreztest] ([name]) output inserted.id VALUES ('solorez') --执行结果: ...
- android之Activity回传数据
约定:当Activity发生跳转时将原来的Activity成为父Activity,将新出现的Activity成为子Activity. 情景设置 下面是个发短信的Activity 当我们点击图中的+按钮 ...
- c/c++模板的定义和实现分开的问题及其解决方案
注意c/c++模板的定义和实现- - 定义一个类一般都是在头文件中进行类声明,在cpp文件中实现,但使用模板时应注意目前的C ...
- Criteria查询之sqlRestriction()的理解
sqlRestriction()的理解 在Criteria查询中 使用sqlRestriction()方法来提供SQL语法作限定查询,作为where字句 查看官方给的例子,如下 List cats = ...
- c# Winform 开发分屏显示应用程序
分屏显示即可把一台主机内运行的多个程序分别显示在不同的两个(或多个)屏幕上.目前市面上主流的显卡都支持分屏显示(显示双屏幕),如果需要显示2个以上的屏幕,则应使用“拖机卡”类的硬件. 设置分屏显示的两 ...
- ios审核要注意的地方(转)
磨刀不误砍柴工.作为手机应用开发者,你需要向应用商店提交应用审核,迅速通过审核可以让你抢占先机.对苹果iOS应用开发者来说尤其如此.苹果应用商店的审核近乎吹毛求疵,下面这些清单可以让你知道苹果会在哪些 ...
- Jquery-分页插件paginaton案例
本例使用的版本为jquery.pagination-1.2.6.js $("#id").page({ showInfo: false, showJump: false, showP ...
- html中拼接字符串问题
hmtl拼接问题: 今天在同事解决问题的时候发现 html <lable id="p"+@item.id></label> 拼接出来的是 p+5 <l ...