Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java CharAt() and deleteCharAt() performance

I've been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what is the complexity of that ? also what about the deleteCharAt() in StringBuffer/StringBuilder ?

like image 710
Jimmar Avatar asked Jun 23 '11 22:06

Jimmar


People also ask

How many parameters we can pass with charAt () method?

The built-in Java string charAt() method returns a character at a particular index position in a string. The first character has the index value 0 and so on for subsequent characters in the string. charAt() accepts one parameter: the index position of the character you want to retrieve.

How do I remove the last character of a StringBuilder in Java?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove. Remove last character of a string using sb. deleteCharAt(str.

What is the time complexity of charAt in Java?

Therefore the time complexity of charAt should be int O(1).

Is charAt slow?

In Java (maybe the same for other langues as well) the charAt(x) method for accessing characters in a string is much slower than accessing the character elements in a char[] .


2 Answers

For String, StringBuffer, and StringBuilder, charAt() is a constant-time operation.

For StringBuffer and StringBuilder, deleteCharAt() is a linear-time operation.

StringBuffer and StringBuilder have very similar performance characteristics. The primary difference is that the former is synchronized (so is thread-safe) while the latter is not.

like image 137
Matt Ball Avatar answered Sep 22 '22 19:09

Matt Ball


Let us just look at the corresponding actual java implementation(only relevant code) for each of these methods in turn. That itself will answer about their efficiency.

String.charAt :

public char charAt(int index) {     if ((index < 0) || (index >= value.length)) {         throw new StringIndexOutOfBoundsException(index);     }     return value[index]; } 

As we can see, it is just a single array access which is a constant time operation.

StringBuffer.charAt :

public synchronized char charAt(int index) {   if ((index < 0) || (index >= count))     throw new StringIndexOutOfBoundsException(index);   return value[index]; } 

Again, single array access, so a constant time operation.

StringBuilder.charAt :

public char charAt(int index) {     if ((index < 0) || (index >= count))         throw new StringIndexOutOfBoundsException(index);     return value[index]; } 

Again, single array access, so a constant time operation. Even though all these three methods look same, there are some minor differences. For example, only StringBuffer.charAt method is synchronized but not other methods. Similarly if check is slightly different for String.charAt (guess why?). Closer look at these method implementations itself give us other minor differences among them.

Now, let us look at deleteCharAt implementations.

String does not have deleteCharAt method. The reason might be it is an immutable object. So exposing an API which explicitly indicates that this method modifies the object is not probably a good idea.

Both StringBuffer and StringBuilder are subclasses of AbstractStringBuilder. The deleteCharAt method of these two classes is delegating the implementation to its parent class itself.

StringBuffer.deleteCharAt :

  public synchronized StringBuffer deleteCharAt(int index) {         super.deleteCharAt(index);         return this;     } 

StringBuilder.deleteCharAt :

 public StringBuilder deleteCharAt(int index) {         super.deleteCharAt(index);         return this;     } 

AbstractStringBuilder.deleteCharAt :

  public AbstractStringBuilder deleteCharAt(int index) {         if ((index < 0) || (index >= count))             throw new StringIndexOutOfBoundsException(index);         System.arraycopy(value, index+1, value, index, count-index-1);         count--;         return this;     } 

A closer look at AbstractStringBuilder.deleteCharAt method reveals that it is actually calling System.arraycopy. This can be O(N) in worst case. So deleteChatAt method is O(N) time complexity.

like image 35
Nagakishore Sidde Avatar answered Sep 21 '22 19:09

Nagakishore Sidde