1 package org.jgroups.tests; 2 3 7 public class StringTest { 8 final int NUM=1000000; 9 long start, stop; 10 11 public static void main(String [] args) { 12 new StringTest().start(); 13 } 14 15 private void start() { 16 rawStringsWithObjects(); 17 rawStringsWithLiterals(); 18 stringBuffer(); 19 } 20 21 22 private void rawStringsWithObjects() { 23 String result=null; 24 String a="a", b="b", c="c", d="d"; 25 long time=System.currentTimeMillis(); 26 start=System.currentTimeMillis(); 27 for(int i=0; i < NUM; i++) { 28 result=a + b + c + d + "ecdsfh" + time; 29 } 30 stop=System.currentTimeMillis(); 31 System.out.println("total time for rawStringsWithObjects(): " + (stop-start)); 32 System.out.println("result=" + result); 33 } 34 35 private void rawStringsWithLiterals() { 36 String result=null; 37 long time=System.currentTimeMillis(); 38 start=System.currentTimeMillis(); 39 for(int i=0; i < NUM; i++) { 40 result="a" + "b" + "c" + "d" + "ecdsfh" + time; } 43 stop=System.currentTimeMillis(); 44 System.out.println("total time for rawStringsWithLiterals(): " + (stop-start)); 45 System.out.println("result=" + result); 46 } 47 48 private void stringBuffer() { 49 String result=null; 50 StringBuffer sb; 51 long time=System.currentTimeMillis(); 52 start=System.currentTimeMillis(); 53 for(int i=0; i < NUM; i++) { 54 sb=new StringBuffer ("a"); 55 sb.append("b").append("c").append("d").append("ecdsfh").append(time); 56 result=sb.toString(); 57 } 58 stop=System.currentTimeMillis(); 59 System.out.println("total time for stringBuffer(): " + (stop-start)); 60 System.out.println("result=" + result); 61 } 62 } 63 | Popular Tags |