欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-18 19:09:34
目录> 源代码,供参考> 测试结果:> 查看源代码,以及简单分析> 字符串拼接一般使用“+”,但是“+”不能满足大批量数据的处理,java中有以下五种方法处理字符串拼接,各有优...

> 字符串拼接一般使用“+”,但是“+”不能满足大批量数据的处理,java中有以下五种方法处理字符串拼接,各有优缺点,程序开发应选择合适的方法实现。

1. 加号 “+”

2. string contact() 方法

3. stringutils.join() 方法

4. stringbuffer append() 方法

5. stringbuilder append() 方法

> 经过简单的程序测试,从执行100次到90万次的时间开销如下表:

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

 由此可以看出:

1. 方法1 加号 “+” 拼接 和 方法2 string contact() 方法 适用于小数据量的操作,代码简洁方便,加号“+” 更符合我们的编码和阅读习惯;

2. 方法3 stringutils.join() 方法 适用于将arraylist转换成字符串,就算90万条数据也只需68ms,可以省掉循环读取arraylist的代码;

3. 方法4 stringbuffer append() 方法 和 方法5 stringbuilder append() 方法 其实他们的本质是一样的,都是继承自abstractstringbuilder,效率最高,大批量的数据处理最好选择这两种方法。

4. 方法1 加号 “+” 拼接 和 方法2 string contact() 方法 的时间和空间成本都很高(分析在本文末尾),不能用来做批量数据的处理。

> 源代码,供参考

package cnblogs.twzheng.lab2;

/**
 * @author tan wenzheng
 *
 */
import java.util.arraylist;
import java.util.list;

import org.apache.commons.lang3.stringutils;

public class teststring {

    private static final int max = 100;

    public void testplus() {
        system.out.println(">>> testplus() <<<");

        string str = "";

        long start = system.currenttimemillis();

        for (int i = 0; i < max; i++) {
            str = str + "a";
        }

        long end = system.currenttimemillis();

        long cost = end - start;

        system.out.println("   {str + \"a\"} cost=" + cost + " ms");
    }

    public void testconcat() {
        system.out.println(">>> testconcat() <<<");

        string str = "";

        long start = system.currenttimemillis();

        for (int i = 0; i < max; i++) {
            str = str.concat("a");
        }

        long end = system.currenttimemillis();

        long cost = end - start;

        system.out.println("   {str.concat(\"a\")} cost=" + cost + " ms");
    }

    public void testjoin() {
        system.out.println(">>> testjoin() <<<");

        long start = system.currenttimemillis();

        list<string> list = new arraylist<string>();

        for (int i = 0; i < max; i++) {
            list.add("a");
        }

        long end1 = system.currenttimemillis();
        long cost1 = end1 - start;

        stringutils.join(list, "");

        long end = system.currenttimemillis();
        long cost = end - end1;

        system.out.println("   {list.add(\"a\")} cost1=" + cost1 + " ms");
        system.out.println("   {stringutils.join(list, \"\")} cost=" + cost
                + " ms");
    }

    public void teststringbuffer() {
        system.out.println(">>> teststringbuffer() <<<");

        long start = system.currenttimemillis();

        stringbuffer strbuffer = new stringbuffer();

        for (int i = 0; i < max; i++) {
            strbuffer.append("a");
        }
        strbuffer.tostring();

        long end = system.currenttimemillis();

        long cost = end - start;

        system.out.println("   {strbuffer.append(\"a\")} cost=" + cost + " ms");
    }

    public void teststringbuilder() {
        system.out.println(">>> teststringbuilder() <<<");

        long start = system.currenttimemillis();

        stringbuilder strbuilder = new stringbuilder();

        for (int i = 0; i < max; i++) {
            strbuilder.append("a");
        }
        strbuilder.tostring();

        long end = system.currenttimemillis();

        long cost = end - start;

        system.out
                .println("   {strbuilder.append(\"a\")} cost=" + cost + " ms");
    }
}

> 测试结果:

1. 执行100次, private static final int max = 100;

>>> testplus() <<<
   {str + "a"} cost=0 ms
>>> testconcat() <<<
   {str.concat("a")} cost=0 ms
>>> testjoin() <<<
   {list.add("a")} cost1=0 ms
   {stringutils.join(list, "")} cost=20 ms
>>> teststringbuffer() <<<
   {strbuffer.append("a")} cost=0 ms
>>> teststringbuilder() <<<
   {strbuilder.append("a")} cost=0 ms

2. 执行1000次, private static final int max = 1000;

>>> testplus() <<<
   {str + "a"} cost=10 ms
>>> testconcat() <<<
   {str.concat("a")} cost=0 ms
>>> testjoin() <<<
   {list.add("a")} cost1=0 ms
   {stringutils.join(list, "")} cost=20 ms
>>> teststringbuffer() <<<
   {strbuffer.append("a")} cost=0 ms
>>> teststringbuilder() <<<
   {strbuilder.append("a")} cost=0 ms

3. 执行1万次, private static final int max = 10000;

>>> testplus() <<<
   {str + "a"} cost=150 ms
>>> testconcat() <<<
   {str.concat("a")} cost=70 ms
>>> testjoin() <<<
   {list.add("a")} cost1=0 ms
   {stringutils.join(list, "")} cost=30 ms
>>> teststringbuffer() <<<
   {strbuffer.append("a")} cost=0 ms
>>> teststringbuilder() <<<
   {strbuilder.append("a")} cost=0 ms

4. 执行10万次, private static final int max = 100000;

>>> testplus() <<<
   {str + "a"} cost=4198 ms
>>> testconcat() <<<
   {str.concat("a")} cost=1862 ms
>>> testjoin() <<<
   {list.add("a")} cost1=21 ms
   {stringutils.join(list, "")} cost=49 ms
>>> teststringbuffer() <<<
   {strbuffer.append("a")} cost=10 ms
>>> teststringbuilder() <<<
   {strbuilder.append("a")} cost=10 ms

5. 执行20万次, private static final int max = 200000;

>>> testplus() <<<
   {str + "a"} cost=17196 ms
>>> testconcat() <<<
   {str.concat("a")} cost=7653 ms
>>> testjoin() <<<
   {list.add("a")} cost1=20 ms
   {stringutils.join(list, "")} cost=51 ms
>>> teststringbuffer() <<<
   {strbuffer.append("a")} cost=20 ms
>>> teststringbuilder() <<<
   {strbuilder.append("a")} cost=16 ms

6. 执行50万次, private static final int max = 500000;

>>> testplus() <<<
   {str + "a"} cost=124693 ms
>>> testconcat() <<<
   {str.concat("a")} cost=49439 ms
>>> testjoin() <<<
   {list.add("a")} cost1=21 ms
   {stringutils.join(list, "")} cost=50 ms
>>> teststringbuffer() <<<
   {strbuffer.append("a")} cost=20 ms
>>> teststringbuilder() <<<
   {strbuilder.append("a")} cost=10 ms

7. 执行90万次, private static final int max = 900000;

>>> testplus() <<<
   {str + "a"} cost=456739 ms
>>> testconcat() <<<
   {str.concat("a")} cost=186252 ms
>>> testjoin() <<<
   {list.add("a")} cost1=20 ms
   {stringutils.join(list, "")} cost=68 ms
>>> teststringbuffer() <<<
   {strbuffer.append("a")} cost=30 ms
>>> teststringbuilder() <<<
   {strbuilder.append("a")} cost=24 ms

> 查看源代码,以及简单分析

string contact 和 stringbuffer,stringbuilder 的源代码都可以在java库里找到,有空可以研究研究。

1. 其实每次调用contact()方法就是一次数组的拷贝,虽然在内存中是处理都是原子性操作,速度非常快,但是,最后的return语句会创建一个新string对象,限制了concat方法的速度。

    public string concat(string str) {
        int otherlen = str.length();
        if (otherlen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = arrays.copyof(value, len + otherlen);
        str.getchars(buf, len);
        return new string(buf, true);
    }

2. stringbuffer 和 stringbuilder 的append方法都继承自abstractstringbuilder,整个逻辑都只做字符数组的加长,拷贝,到最后也不会创建新的string对象,所以速度很快,完成拼接处理后在程序中用strbuffer.tostring()来得到最终的字符串。

    /**
     * appends the specified string to this character sequence.
     * <p>
     * the characters of the {@code string} argument are appended, in
     * order, increasing the length of this sequence by the length of the
     * argument. if {@code str} is {@code null}, then the four
     * characters {@code "null"} are appended.
     * <p>
     * let <i>n</i> be the length of this character sequence just prior to
     * execution of the {@code append} method. then the character at
     * index <i>k</i> in the new character sequence is equal to the character
     * at index <i>k</i> in the old character sequence, if <i>k</i> is less
     * than <i>n</i>; otherwise, it is equal to the character at index
     * <i>k-n</i> in the argument {@code str}.
     *
     * @param   str   a string.
     * @return  a reference to this object.
     */
    public abstractstringbuilder append(string str) {
        if (str == null) str = "null";
        int len = str.length();
        ensurecapacityinternal(count + len);
        str.getchars(0, len, value, count);
        count += len;
        return this;
    }

    /**
     * this method has the same contract as ensurecapacity, but is
     * never synchronized.
     */
    private void ensurecapacityinternal(int minimumcapacity) {
        // overflow-conscious code
        if (minimumcapacity - value.length > 0)
            expandcapacity(minimumcapacity);
    }

    /**
     * this implements the expansion semantics of ensurecapacity with no
     * size check or synchronization.
     */
    void expandcapacity(int minimumcapacity) {
        int newcapacity = value.length * 2 + 2;
        if (newcapacity - minimumcapacity < 0)
            newcapacity = minimumcapacity;
        if (newcapacity < 0) {
            if (minimumcapacity < 0) // overflow
                throw new outofmemoryerror();
            newcapacity = integer.max_value;
        }
        value = arrays.copyof(value, newcapacity);
    }

3. 字符串的加号“+” 方法, 虽然编译器对其做了优化,使用stringbuilder的append方法进行追加,但是每循环一次都会创建一个stringbuilder对象,且都会调用tostring方法转换成字符串,所以开销很大。

  注:执行一次字符串“+”,相当于 str = new stringbuilder(str).append("a").tostring();

4. 本文开头的地方统计了时间开销,根据上述分析再想想空间的开销。常说拿空间换时间,反过来是不是拿时间换到了空间呢,但是在这里,其实时间是消耗在了重复的不必要的工作上(生成新的对象,tostring方法),所以对大批量数据做处理时,加号“+” 和 contact 方法绝对不能用,时间和空间成本都很高。

到此这篇关于java字符串拼接的五种方法及性能比较分析(从执行100次到90万次)的文章就介绍到这了,更多相关java字符串拼接内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!