StringTest1.java package com.performance.string; /** This class shows the time taken for creation of * String literals and String objects. */ public class StringTest1 { public static void main(String[] args){ // create String literals long startTime = System.currentTimeMillis(); for(int i=0;i<50000;i++){ String s1 = "hello"; String s2 = "hello"; } long endTime = System.currentTimeMillis(); System.out.PRintln("Time taken for creation of String literals : " + (endTime - startTime) + " milli seconds" ); // create String objects using 'new' keyWord long startTime1 = System.currentTimeMillis(); for(int i=0;i<50000;i++){ String s3 = new String("hello"); String s4 = new String("hello"); } long endTime1 = System.currentTimeMillis(); System.out.println("Time taken for creation of String objects : " + (endTime1 - startTime1)+" milli seconds"); } } 这段代码的输出: Time taken for creation of String literals : 0 milli seconds Time taken for creation of String objects : 170 milli seconds
package com.performance.string; // This class shows the use of intern() method to improve performance public class StringTest2 { public static void main(String[] args){ // create String references like s1,s2,s3...so on.. String variables[] = new String[50000]; for( int i=0;i<variables.length;i++){ variables[i] = "s"+i; } // create String literals long startTime0 = System.currentTimeMillis(); for(int i=0;i<variables.length;i++){ variables[i] = "hello"; } long endTime0 = System.currentTimeMillis(); System.out.println("Time taken for creation of String literals : " + (endTime0 - startTime0) + " milli seconds" ); // create String objects using 'new' keyword long startTime1 = System.currentTimeMillis(); for(int i=0;i<variables.length;i++){ variables[i] = new String("hello"); } long endTime1 = System.currentTimeMillis(); System.out.println("Time taken for creation of String objects with 'new' key word : " + (endTime1 - startTime1)+" milli seconds"); // intern String objects with intern() method long startTime2 = System.currentTimeMillis(); for(int i=0;i<variables.length;i++){ variables[i] = new String("hello"); variables[i] = variables[i].intern(); } long endTime2 = System.currentTimeMillis(); System.out.println("Time taken for creation of String objects with intern(): " + (endTime2 - startTime2)+" milli seconds"); } } 这是上面那段代码的输出结果: Time taken for creation of String literals : 0 milli seconds Time taken for creation of String objects with 'new' key word : 160 milli seconds Time taken for creation of String objects with intern(): 60 milli seconds
package com.performance.string; /** This class shows the time taken by string concatenation at compile time and run time.*/ public class StringTest3 { public static void main(String[] args){ //Test the String Concatination long startTime = System.currentTimeMillis(); for(int i=0;i<5000;i++){ String result = "This is"+ "testing the"+ "difference"+ "between"+ "String"+ "and"+ "StringBuffer"; } long endTime = System.currentTimeMillis(); System.out.println("Time taken for string concatenation using + Operator : " + (endTime - startTime)+ " milli seconds"); //Test the StringBuffer Concatination long startTime1 = System.currentTimeMillis(); for(int i=0;i<5000;i++){ StringBuffer result = new StringBuffer(); result.append("This is"); result.append("testing the"); result.append("difference"); result.append("between"); result.append("String"); result.append("and"); result.append("StringBuffer"); } long endTime1 = System.currentTimeMillis(); System.out.println("Time taken for String concatenation using StringBuffer : " + (endTime1 - startTime1)+ " milli seconds"); } } 这是上面的代码的输出结果: Time taken for String concatenation using + operator : 0 milli seconds Time taken for String concatenation using StringBuffer : 50 milli seconds 很有趣地,+操作符居然比StringBuffer.append()方法要快,为什么呢?
编译前: String result = "This is"+"testing the"+"difference"+"between"+"String"+"and"+"StringBuffer"; 编译后: String result = "This is testing the difference between String and StringBuffer";
package com.performance.string; /** This class shows the time taken by string concatenation using + operator and StringBuffer */ public class StringTest4 { public static void main(String[] args){ //Test the String Concatenation using + operator long startTime = System.currentTimeMillis(); String result = "hello"; for(int i=0;i<1500;i++){ result += "hello"; } long endTime = System.currentTimeMillis(); System.out.println("Time taken for string concatenation using + operator : " + (endTime - startTime)+ " milli seconds"); //Test the String Concatenation using StringBuffer long startTime1 = System.currentTimeMillis(); StringBuffer result1 = new StringBuffer("hello"); for(int i=0;i<1500;i++){ result1.append("hello"); } long endTime1 = System.currentTimeMillis(); System.out.println("Time taken for string concatenation using StringBuffer : " + (endTime1 - startTime1)+ " milli seconds"); } } 这是上面的代码的输出结果: Time taken for string concatenation using + operator : 280 milli seconds Time taken for String concatenation using StringBuffer : 0 milli seconds 看得出StringBuffer.append()方法要比+操作符要快得多,为什么呢?
输出结果如下: Time taken for String concatenation using StringBuffer with out setting size: 280 milli seconds Time taken for String concatenation using StringBuffer with setting size: 0 milli seconds StringBuffer初始化过程的调整的作用由此可见一斑。所以,使用一个合适的容量值来初始化StringBuffer永远都是一个最佳的建议。