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();
        }

    }
View Code

 

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

TagAvg(ms)MinMaxStd DevCount
StringAddConcat9355.4786010046547.710
StringBufferConcat3.5052.310
StringBuilderConcat2.0052.410
StringConcat3.1062.510


 

转载于:https://www.cnblogs.com/kissfu/p/4105635.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值