Performance Comparison of String, StringBuffer and StringBuilder
As we know that in Java String objects are immutable objects. It means once created it cannot be changed, only the reference will point to new object.Any application is having string manipulation activity, then there will be many unused string objects in heap memory which might result in performance impact.
To overcome from this issue , we can use either the StringBuilder or StringBuffer class. we can use one of these string related classes, which are alternatives to the String class. Whenever we know that the String objects is going to modify then we can go for StringBuffer or StringBuilder classes. Usually, we can use
StringBuffer or StringBuilder object anywhere instead of using String.
StringBuffer objects are very flexible, it may have characters and substrings inserted in the middle or appended to the end. StringBuffer will automatically grow to make space for such additions and often has more characters pre-allocated than are actually needed, to allow space for growth. These two classes are also the part of the java.lang package and are automatically imported into every program. Features of these classes apart from the string manipulation related features
· StringBuilder is more efficient.
· StringBuffer is thread safe because of all synchronized methods.
Using the below program, we will try to know about the performance efficiency amoung String, StringBuffer and StringBuilder objects.
In the below program, we are creating new string objects inside the for loop then in the second for loop we are creating StringBuffer objects and in the third for loop we are creating StringBuilder objects. Execution time for objects creation is calculated and displayed in millisecond.
Example to judge the performance the String, StringBuffer and StringBuilder
package com.gaurav.performancetest;
import java.util.Date;
import java.sql.Timestamp;
public classPerformanceComparison {
publicstatic void main(String[] args) {
// Process for String objects
Date stringStartDate = new Date();
longstringStartTime = stringStartDate.getTime();
System.out.println("String Object Creation started : "
+ new Timestamp(stringStartTime));
String stringVar = new String("KUMAR GAURAV");
for(int i = 0; i < 15000; i++) {
stringVar += i;
}
Date stringEndDate = new Date();
longstringEndTime = stringEndDate.getTime();
System.out.println("String Object Creation completed : "
+ new Timestamp(stringEndTime));
System.out
.println("Complete Execution time to process string object creation : "
+ (stringEndTime - stringStartTime) + "ms");
System.out
.println("****************************************************************");
// Process for StringBuffer objects
Date stringBufferStartDate = new Date();
longstringBufferStartTime = stringBufferStartDate.getTime();
System.out.println("StringBuffer Object Creation started : "
+ new Timestamp(stringBufferStartTime));
StringBuffer stringBufferVar = new StringBuffer("KUMAR GAURAV");
for(int i = 0; i < 15000; i++) {
stringBufferVar.append(i);
}
Date stringBufferEndDate = new Date();
longstringBufferEndTime = stringBufferEndDate.getTime();
System.out.println("StringBuffer Object Creation completed : "
+ new Timestamp(stringBufferEndTime));
System.out
.println("Complete Execution time to process StringBuffer object creation "
+ (stringBufferEndTime - stringBufferStartTime) + "ms");
System.out
.println("****************************************************************");
// Process for StringBuilder objects
Date stringBuilderStartDate = new Date();
longstringBuilderStartTime = stringBuilderStartDate.getTime();
System.out.println("StringBuilder Object Creation started : "
+ new Timestamp(stringBuilderStartTime));
StringBuilder stringBuilderVar = new StringBuilder("KUMAR GAURAV");
for(int i = 0; i < 15000; i++) {
stringBuilderVar.append(i);
}
Date stringBuilderEndDate = new Date();
longstringBuilderEndTime = stringBuilderEndDate.getTime();
System.out.println("StringBuilder Object Creation completed : "
+ new Timestamp(stringBuilderEndTime));
System.out
.println("Complete Execution time to process StringBuilder object creation "
+ (stringBuilderEndTime - stringBuilderStartTime)
+ "ms");
System.out
.println("****************************************************************");
}
}
Result:-
String Object Creation started : 2013-08-08 11:19:43.343
String Object Creation completed : 2013-08-08 11:19:43.936
Complete Execution time to process string object creation : 593ms
****************************************************************
StringBuffer Object Creation started : 2013-08-08 11:19:43.936
StringBuffer Object Creation completed : 2013-08-08 11:19:43.938
Complete Execution time to process StringBuffer object creation 2ms
****************************************************************
StringBuilder Object Creation started : 2013-08-08 11:19:43.938
StringBuilder Object Creation completed : 2013-08-08 11:19:43.939
Complete Execution time to process StringBuilder object creation 1ms
****************************************************************
Note:- After the execution of the above program we will find that, String objects are slightly slower in performance due to new object creation and others are working fast.
- String objects are immutable due to this it makes performance of application bit slower
- Java API provides StringBuffer and StringBuilder classes for improving the string manipulation operation in the application.
- StringBuffer and StringBuilder are similar classes, only difference is that all methods are synchronized in StringBuffer while StringBuilder has non-synchronized methods.
No comments:
Post a Comment