CSDN博客

img yoti

String和StringBuffer

发表于2004/10/1 23:56:00  532人阅读

分类: JAVA

刚刚看到一篇讨论String和StringBuffer的文章,使我又更加的了解了它们。
http://community.csdn.net/Expert/topic/2872/2872127.xml
测试代码如下:

import java.util.Date;

public class TestStringPerformance {
  static final int MAX_APPEND = 5000;
  static final int THREAD_SLEEP = 6000;
 
  public static void main(String[] args) {
    long start, end;
   
    System.gc();
    try {Thread.sleep(THREAD_SLEEP);}catch(InterruptedException ie){}
    start = new Date().getTime();
    Method1();
    end = new Date().getTime();
    System.out.println("Method1 took " + (end-start) + " milliseconds.");

    System.gc();
    try {Thread.sleep(THREAD_SLEEP);}catch(InterruptedException ie){}
    start = new Date().getTime();
    Method2();
    end = new Date().getTime();
    System.out.println("Method2 took " + (end-start) + " milliseconds.");

    System.gc();
    try {Thread.sleep(THREAD_SLEEP);}catch(InterruptedException ie){}
    start = new Date().getTime();
    Method3();
    end = new Date().getTime();
    System.out.println("Method3 took " + (end-start) + " milliseconds.");

    System.gc();
    try {Thread.sleep(THREAD_SLEEP);}catch(InterruptedException ie){}
    start = new Date().getTime();
    Method4();
    end = new Date().getTime();
    System.out.println("Method4 took " + (end-start) + " milliseconds.");
  }
 
  // new keyword, no StringBuffers
  static String Method1() {
    String res = new String();
    for (int i = 0; i < MAX_APPEND; i++) {
      int temp = i%5;
      switch(temp) {
        case 0 : res += new String("AA"); break;
        case 1 : res += new String("BB"); break;
        case 2 : res += new String("CC"); break;
        case 3 : res += new String("DD"); break;
        case 4 : res += new String("EE"); break;
      }
    }
    return res;
  }
 
  // no new keywords, no StringBuffers
  static String Method2() {
    String res = "";
    for (int i = 0; i < MAX_APPEND; i++) {
      int temp = i%5;
      switch(temp) {
        case 0 : res += "AA"; break;
        case 1 : res += "BB"; break;
        case 2 : res += "CC"; break;
        case 3 : res += "DD"; break;
        case 4 : res += "EE"; break;
      }
    }
    return res;
  }
 
  // new keyword, StringBuffer used
  static String Method3() {
    StringBuffer res = new StringBuffer();
    for (int i = 0; i < MAX_APPEND; i++) {
      int temp = i%5;
      switch(temp) {
        case 0 : res.append(new String("AA")); break;
        case 1 : res.append(new String("BB")); break;
        case 2 : res.append(new String("CC")); break;
        case 3 : res.append(new String("DD")); break;
        case 4 : res.append(new String("EE")); break;
      }
    }
    return res.toString();
  }

  // no new keywords, StringBuffer used
  static String Method4() {
    StringBuffer res = new StringBuffer();
    for (int i = 0; i < MAX_APPEND; i++) {
      int temp = i%5;
      switch(temp) {
        case 0 : res.append("AA"); break;
        case 1 : res.append("BB"); break;
        case 2 : res.append("CC"); break;
        case 3 : res.append("DD"); break;
        case 4 : res.append("EE"); break;
      }
    }
    return res.toString();
  }

}


测试结果:
Method1 took 901 milliseconds.
Method2 took 811 milliseconds.
Method3 took 10 milliseconds.
Method4 took 0 milliseconds.

说明:
Method1使用new但是没用StringBuffer
Method2没用new也没有用StringBuffer
Method3使用new也使用了StringBuffer
Method4没用new但是用了StringBuffer

由此看来,在进行大量字符串运算的时候,我们选用StringBuffer能够大大提高性能.

阅读全文
0 0

相关文章推荐

img
取 消
img