zoukankan      html  css  js  c++  java
  • 几种拼接字符串的效率问题 .

    1. public class test {   
    2.    
    3. /**  
    4. * @param args  
    5. */   
    6. public static void main(String[] args) {   
    7. // TODO Auto-generated method stub   
    8.         long n = 30000;  
    9.         System.out.println("Start... "+n);    
    10.           
    11.         long start1 = System.currentTimeMillis();     
    12.         String s1 = new String("hello");     
    13.         for (long i = 0; i < n; i++)     
    14.         {     
    15.             s1+="拼接字符串的时间";     
    16.         }     
    17.         long end1 = System.currentTimeMillis();     
    18.         long time1 = end1 -start1;     
    19.         System.out.println("用String+=拼接字符串的时间"+time1);     
    20.              
    21.         long start2 = System.currentTimeMillis();     
    22.         String s2 = new String("hello");     
    23.         for (long i = 0; i < n; i++)     
    24.         {     
    25.             s2=s2+"拼接字符串的时间";     
    26.         }     
    27.         long end2 = System.currentTimeMillis();     
    28.         long time2 = end2 -start2;     
    29.         System.out.println("用String=String+拼接字符串的时间"+time2);   
    30.           
    31.         long start3 = System.currentTimeMillis();     
    32.         String s3 = new String("hello");     
    33.         for (long i = 0; i < n; i++)     
    34.         {     
    35.             s3=s3.concat("拼接字符串的时间");     
    36.         }     
    37.         long end3 = System.currentTimeMillis();     
    38.         long time3 = end3 -start3;     
    39.         System.out.println("用String.concat拼接字符串的时间"+time3);   
    40.           
    41.         long start4 = System.currentTimeMillis();     
    42.         StringBuffer s4 = new StringBuffer("hello");     
    43.         for (long i = 0; i < n; i++)     
    44.         {     
    45.             s4.append("拼接字符串的时间");     
    46.         }     
    47.         long end4 = System.currentTimeMillis();     
    48.         long time4 = end4 -start4;     
    49.         System.out.println("用StringBuffer.append拼接字符串的时间"+time4);     
    50.              
    51.         long start5 = System.currentTimeMillis();     
    52.         StringBuilder s5 = new StringBuilder("hello");     
    53.         for (long i = 0; i < n; i++)     
    54.         {     
    55.             s5.append("拼接字符串的时间");     
    56.         }     
    57.         long end5 = System.currentTimeMillis();     
    58.         long time5 = end5 -start5;     
    59.         System.out.println("用StringBuilder.append拼接字符串的时间"+time5);     
    60.           
    61.         System.out.println("End...");    
    62. }   
    63.    
    64. }   



     

    贴出一组检测数据如下:

     

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. 用String+=拼接字符串的时间27468  
    2. 用String=String+拼接字符串的时间25813  
    3. 用String.concat拼接字符串的时间12265  
    4. 用StringBuffer.append拼接字符串的时间14  
    5. 用StringBuilder.append拼接字符串的时间8  
  • 相关阅读:
    cookie操作
    css加载动画...
    三目运算符的运用
    遍历对象长度
    2年
    相亲

    股市周期
    功利心
    思考笔记
  • 原文地址:https://www.cnblogs.com/gc2013/p/4351015.html
Copyright © 2011-2022 走看看