zoukankan      html  css  js  c++  java
  • java String拼接的方法选择及性能分析

    String 拼接的方法选择

    在拼接静态字符串时,尽量用 +,因为通常编译器会对此做优化,如:

     String test = "this " + "is " + "a " + "test " + "string"

    编译器会把它视为:

     String test = "this is a test string"

    在拼接动态字符串时,尽量用 StringBuffer 或 StringBuilder的 append,这样可以减少构造过多的临时 String 对象。

    测试代码:(按照附录1修改)

     1 public class teststring {
     2    
     3     public void testPlus() {
     4         String s = "";
     5         long ts = System.currentTimeMillis();
     6         for (int i = 0; i < 10000; i++) {
     7             s = s + String.valueOf(i);
     8         }
     9         long te = System.currentTimeMillis();
    10         System.out.println("+ cost {" + ( te - ts) + "} ms");
    11     }
    12     
    13     public void testConcat() {
    14         String s = "";
    15         long ts = System.currentTimeMillis();
    16         for (int i = 0; i < 10000; i++) {
    17             s = s.concat(String.valueOf(i));
    18         }
    19         long te = System.currentTimeMillis();
    20         System.out.println("concat cost {" + (te - ts) + "} ms");
    21     }
    22 
    23     
    24     public void testStringBuffer() {
    25         StringBuffer sb = new StringBuffer();
    26         long ts = System.currentTimeMillis();
    27         for (int i = 0; i < 10000; i++) {
    28             sb.append(String.valueOf(i));
    29         }
    30         sb.toString();
    31         long te = System.currentTimeMillis();
    32         System.out.println("StringBuffer cost {" + (te - ts) + "} ms");
    33     }
    34     
    35     public void testStringBuilder() {
    36         StringBuilder sb = new StringBuilder();
    37         long ts = System.currentTimeMillis();
    38         for (int i = 0; i < 100000; i++) {
    39             sb.append(String.valueOf(i));
    40         }
    41         sb.toString();
    42         long te = System.currentTimeMillis();
    43         System.out.println("StringBuilder cost {" + (te - ts) + "} ms");
    44     }
    45     
    46     public static void main(String[] args) {
    47         teststring a = new teststring();
    48         a.testConcat();
    49         a.testPlus();
    50         a.testStringBuffer();
    51         a.testStringBuilder();
    52     }
    53 }

    运行结果:

    concat cost {} ms 113
    + cost {} ms 195
    StringBuffer cost {} ms 2
    StringBuilder cost {} ms 9

    可见 存在大量的拼接动态字符串操作时,尽量用 StringBuffer 或 StringBuilder的 append。使用'+'运算符的开销是不可忍受的。

    In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

    Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

    附录:

    Java 5种字符串拼接方式性能比较 http://blog.csdn.net/kimsoft/article/details/3353849

    2 Java 性能优化之 String 篇  http://www.ibm.com/developerworks/cn/java/j-lo-optmizestring/

  • 相关阅读:
    没人比程序猿更讨厌软件
    随心所欲~我也做个集合遍历器吧(自己的foreach,委托的威力)
    EF架构~DefaultValue让我的UnitOfWork更可读
    EF架构~数据分批批量提交
    陷阱~EF中的Update与Insert共用一个数据上下文
    我心中的核心组件(可插拔的AOP)~第四回 异常拦截器
    zigbee学习:示例程序SampleApp中通讯流程
    【网络可靠版】Extjs4 Treegrid 使用实例
    C#实现微信公众号群发消息(解决一天只能发一次的限制)
    oracle12c(oracle12.1.0.1.0)安装指南--实测OEL5.9(RH5)
  • 原文地址:https://www.cnblogs.com/scw2901/p/4344884.html
Copyright © 2011-2022 走看看