`
sunxboy
  • 浏览: 2877502 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Performance improvement techniques in String and StringBuffer

 
阅读更多

This topic illustrates the performance improvement techniques in String and StringBuffer with the following sections:

Overview of String and StringBuffer

Immutable objects cannot be modified once they are created. Mutable objects can be modified after their creation. String objects are immutable where as StringBuffer objects are mutable. 
You need to carefully choose between these two objects depending on the situation for better performance. The following topics illustrate in detail :

Note: This section assumes that reader has some basic knowledge of Java Strings and StringBuffer.

Better way of creating Strings

You can create String objects in the following ways.

1.String s1 = "hello";	
String s2 = "hello";	
2.String s3 = new String("hello");
  String s4 = new String("hello");

Which of the above gives better performance?

Here is a code snippet to measure the difference.

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");

         }

}

 

 

 

The output of this code

 

Time taken for creation of String literals : 0 milli seconds

Time taken for creation of String objects : 170 milli seconds

 

 

 

It clearly shows first type of creation is much more faster than second type of creation. Why?

Because the content is same s1 and s2 refer to the same object where as s3 and s4 do not refer to the same object. The 'new' key word creates new objects for s3 and s4 which is expensive.

 

How the JVM works with Strings:

Java Virtual Machine maintains an internal list of references for interned Strings ( pool of unique Strings) to avoid duplicate String objects in heap memory. Whenever the JVM loads String literal from class file and executes, it checks whether that String exists in the internal list or not. If it already exists in the list, then it  does not create a new String and it uses reference to the existing String Object. JVM does this type of checking internally for String literal but not for String object which it creates through 'new' keyword. You can explicitly force JVM to do this type of checking for String objects which are created through 'new' keyword using String.intern() method. This forces JVM to check the internal list and use the existing String object if it is already present.

So the conclusion is, JVM maintains unique String objects for String literals internally. Programmers need not bother about String literals but they should bother about String objects that are created using 'new' keyword and they should use intern() method to avoid duplicate String objects in heap memory which in turn improves java performance. see the following section for more information.

The following figure shows the creation of String Objects without using the intern() method.


You can test the above difference programmatically using == operator and String.equals() method.

== operator returns true if the references point to the same object but it does not check the contents of the String object where as String.equals() method returns true if the contents of the String objects are equal.

s1==s2 for the above code returns true because s1 and s2 references point to the same object.

s3.equals(s4) for the above code returns true because both objects content  is same which is "hello".

You can see this mechanism in the above figure. Here, we have three separate objects which contain same content,"hello". Actually we don't need separate objects because they use memory and take time to execute.

How do you make sure that the String objects are not duplicated?

The next topic covers this interesting interning String mechanism.

 

Optimization by Interning Stings

In situations where String objects are duplicated unnecessarily, String.intern() method avoids duplicating String objects. The following figure shows how the String.intern() method works. The String.intern() method checks the object existence and if the object exists already, it changes point of reference to the original object rather than create a new object.

The following figure shows the creation of String literal and String Object using intern

 

Here is the sample code to know the importance of String.intern() method..

StringTest2.java

 

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");

          }

}

 

 

 

Here is the output of the above code

 

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

 

 

Optimization techniques when Concatenating Strings

You can concatenate multiple strings using either + operator or String.concat()  or   StringBuffer.append(). Which is the best one interms of performance?

The choice depends on two scenarios,first scenario is compile time resolution versus run time resolution and second scenario is wether you are using StringBuffer or String. In general, programmers think that StringBuffer.append() is better than + operator or String.concat() method. But this assumption is not true under certain conditions.

1) First scenario: compile time resolution versus run time resolution

Look at the following code StringTest3.java and the output.

 

 

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");

 }

}

 

 

 

 

The output of this code

 

Time taken for String concatenation using + operator : 0 milli seconds
Time taken for String concatenation using StringBuffer : 50 milli seconds

 

 

 

Interestingly the + operator is faster than StringBuffer.append() method. Let us see why?

Here the compiler does a good job of optimization. Compiler simply concatenates at compile time as shown below. It does compile time resolution instead of runtime resolution, this happens when you create a String object using 'new' key word.

before compilation:

String result = "This is"+"testing the"+"difference"+"between"+"String"+"and"+"StringBuffer";

after compilation

String result = "This is testing the difference between String and StringBuffer";

String object is resolved at compile time where as StringBuffer object is resolved at run time. Run time resolution takes place when the value of the string is not known in advance where as compile time resolution happens when the value of the string is known in advance. Here is an example.

Before compilation:

public String getString(String str1,String str2) {

            return str1+str2;

}

After compilation:

           return new StringBuffer().append(str1).append(str2).toString();

This resolves at run time and take much more time to execute.

 

2) Second scenario: Using StringBuffer instead of String

If you look at the following code, you will find StringBuffer is faster than String for concatenation which is opposite to above scenario.

StringTest4.java

 

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");

          }

}

 

 

 

the output of the code is

 

Time taken for string concatenation using + operator : 280 milli seconds
Time taken for String concatenation using StringBuffer : 0 milli seconds

It shows StringBuffer.append() is much more faster than String. Why?

The reason is both resolve at runtime but the + operator resolves in a different manner and uses String and StringBuffer to do this operation.

 

Optimization by initializing StringBuffer

You can set the initial capacity of StringBuffer using its constructor this improves performance significantly. The constructor is StringBuffer(int length), length shows the number of characters the StringBuffer can hold.

You can even set the capacity using ensureCapacity(int minimumcapacity) after creation of StringBuffer object. Initially we will look at the default behavior and then the better approach later.

The default behavior of StringBuffer:

StringBuffer maintains a character array internally.When you create StringBuffer with default constructor StringBuffer() without setting initial length, then the StringBuffer is initialized with 16 characters. The default capacity is 16 characters. When the StringBuffer reaches its maximum capacity, it will increase its size by twice the size plus 2 ( 2*old size +2).

 If you use default size, initially and go on adding characters, then it increases its size by 34(2*16 +2) after it adds 16th character and it increases its size by 70(2*34+2) after it adds 34th character. Whenever it reaches its maximum capacity it has to create a new character array and recopy old and new characters. It is obviously expensive. So it is always good to initialize with proper size that gives very good performance.

I tested the above StringTest4.java again with two StringBuffers, one without initial size and other with initial size. I added 50000 'hello' objects this time and did not use the + operator. I initialized the second StringBuffer with StringBuffer(250000).

The output is 

 

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

 

 

It shows how effective the initialization of StringBuffer is. So it is always best to initialize the StringBuffer with proper size.

 

Key Points

 

 
  1. Create strings as literals instead of creating String objects using 'new' key word whenever possible
  2. Use String.intern() method if you want to add number of equal objects whenever you   create String objects using 'new' key word.
  3. + operator gives best performance for String concatenation if Strings resolve at compile time
  4. StringBuffer with proper initial size gives best performance for String concatenation if Strings resolve at run time.

 

 

<iframe id="aswift_1" style="left: 0px; position: absolute; top: 0px;" name="aswift_1" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="160" height="600"></iframe>

 

 

分享到:
评论

相关推荐

    String StringBuffer和StringBuilder区别之源码解析

    "String StringBuffer和StringBuilder区别之源码解析" 在Java中,字符串是我们经常使用的数据类型,而String、StringBuffer和StringBuilder是Java中三种常用的字符串类。在这篇文章中,我们将从源码角度对String、...

    String ,StringBuffer与StringBuilder

    String, StringBuffer 与 StringBuilder 的区别 在 Java 中,String, StringBuffer 和 StringBuilder 三个类都是用于字符操作的,但它们之间有着很大的区别。 首先,String 是不可变类,意味着一旦创建了 String ...

    javaString 和StringBuffer的区别

    JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据.这个String类提供了数值不可改变的字符串.而这个StringBuffer类提供的字符串进行修改.当你知道字符数据要改变的时候...

    String、StringBuffer、StringBuilder的使用方法

    在Java编程语言中,`String`、`StringBuffer`和`StringBuilder`是处理字符串的三个重要类,它们各自有特定的使用场景和优缺点。理解它们的差异对于编写高效的代码至关重要。 **String类** `String`是不可变的类,...

    String、StringBuffer与StringBuilder之间区别

     StringBuffer:字符创变量  StringBuilder:字符创变量  从上面的名字可以看到,String是“字符创常量”,也就是不可改变的对象。对于这句话的理解你可能会产生这样一个疑问 ,比如这段代码:

    String及StringBuffer和StringBuilder的区别

    但是,在某些特别情况下,String 对象的字符串拼接其实是被 JVM 解释成了 StringBuffer 对象的拼接,所以这些时候 String 对象的速度并不会比 StringBuffer 对象慢。 线程安全性 StringBuffer 是线程安全的,而 ...

    String 和StringBuffer的区别

    从非常细致的角度分析了String和Stringbuffer的主要区别

    String和StringBuffer的区别

    在Java编程语言中,`String`和`StringBuffer`都是用来表示和操作字符串的重要类,但它们在性能和使用场景上有显著的区别。 首先,`String`类是不可变的,这意味着一旦创建了一个`String`对象,它的内容就不能改变。...

    string和StringBuffer的区别

    在Java编程语言中,`String`和`StringBuffer`都是用来表示和操作字符串的重要类,但它们在使用场景和性能上有显著的区别。了解这些差异对于编写高效、优化的代码至关重要。 首先,`String`类是不可变的。这意味着...

    String-StringBuffer-StringBuilder

    在Java编程语言中,`String`、`StringBuffer`和`StringBuilder`都是用来处理字符串的类,但它们之间存在显著的差异,主要体现在性能、线程安全性和使用场景上。 首先,`String`是最基本的字符串类,它代表的是不可...

    String与StringBuffer区别详解

    在Java编程语言中,`String`和`StringBuffer`(在Java 5之后被`StringBuilder`取代,但在多线程环境中仍然使用`StringBuffer`)是处理文本字符串的两个核心类,它们各自有着不同的特性和用途。理解它们的区别对于...

    JAVA中String与StringBuffer的区别 自己的学习笔记总结

    ### JAVA中String与StringBuffer的区别 在Java编程语言中,`String` 和 `StringBuffer` 都是非常重要的类,它们用于处理字符串数据。虽然它们都实现了字符串操作的功能,但它们之间存在一些重要的区别,这些区别...

    string与stringbuffer的区别

    字符串在Java中的表现形式主要有两种:`String`和`StringBuffer`(或者`StringBuilder`)。它们之间的主要区别在于可变性、性能和使用场景。 1. **不可变性**: `String`类是不可变的,这意味着一旦创建了一个`...

    java面试笔试题String 和StringBuffer的区别

    在Java编程语言中,`String`与`StringBuffer`是两个重要的字符串处理类,它们各自具有独特的特性和用途,尤其在面试或笔试中常被提及作为考察应聘者对Java基础知识掌握程度的重要知识点。 ### `String`类:不可变性...

    源码string和stringbuffer

    针对String的详细介绍针对String的详细介绍针对String的详细介绍针对String的详细介绍针对String的详细介绍针对String的详细介绍针对String的详细介绍针对String的详细介绍针对String的详细介绍针对String的详细介绍...

    String、StringBuilder和StringBuffer的区别

    在Java编程语言中,String、StringBuilder和StringBuffer都是用来处理字符串的类,它们之间存在一些重要的区别,主要涉及到性能和线程安全性。 首先,`String`类代表的是字符串常量,一旦创建,其内容就不能改变。...

    String和StringBuffer详细用法总结

    根据给定的信息,本文将对`String`与`StringBuffer`在Java中的使用进行详细的解析与对比,并结合实例深入分析二者的异同及其适用场景。 ### `String` 和 `StringBuffer` 的基本概念 #### `String` 在Java中,`...

    string,stringBuffer,stringBuilder

    string,stringBuffer,stringBuilder

Global site tag (gtag.js) - Google Analytics