Performance Tip: Avoid String operations which don't do anything.

Overview

There are a number of String operations which don't do anything. Avoiding these may improve performance and clarity as well.

Avoid String.substring(0)

Even though this method doesn't do anything it can be confusing.

From Mlet.removeSpace(String)
String temp = "";
 int k = 0;
 while (j != -1) {
     s = s.substring(k);
     j = s.indexOf(' ');
     if (j != -1) {
     temp = temp + s.substring(0, j);
     } else {
     temp = temp + s.substring(0);
     }
     k = j + 1;
 }
 return temp;
This attempts to optimise the search by using indexOf but suffers from using + to build a string.

The else block can be replaced with
temp = temp + s;
or even
temp += s;

If you look at the whole block of code, the simplest way to do the following which might not be slower.
return s.replaceAll(" ","");

For speed a better option is using a StringBuilder
StringBuilder sb = new StringBuilder(s.length());
for(int i=0, len=s.length(); i < len; i++) {
    char ch = s.charAt(s);
    if (ch != ' ')
       sb.append(ch);
}
return sb.toString();
Using indexOf is usually faster than a manual search. To use it here requires String.substring() which creates an object each time.

Don't call String.toString()

String.toString() returns the same String. It could throw a NullPointerException instead, but it not obvious you want this behaviour. From CorbaResourceUtil.getText()
String[] args = new String[3];
args[0] = (arg0 != null ? arg0.toString() : "null");
args[1] = (arg1 != null ? arg1.toString() : "null");
args[2] = (arg2 != null ? arg2.toString() : "null");

return java.text.MessageFormat.format(format, args);
Calling String.toString() doesn't do anything here and the code could be simplified with
args[0] = (arg0 != null ? arg0 : "null");
There is a method which returns "null" for null already.
args[0] = String.valueOf(arg0);
But what happens if you don't do this and pass null to MessageFormat? It turns it into "null" for you. So all you need is
String[] args = { arg0, arg1, arg2 };
return MessageFormat.format(format, args);
Also format() takes a varags so you can do
return MessageFormat.format(format, arg0, arg1, arg2);

Avoid new String()

This is much simpler and more efficient if you replace it with an empty String literal. From URI.intialize()
String path = new String();
This creates a new String object every time even though Strings with the same contents are all equals().
String path = "";
This is shorter and faster.

Avoid new String(String)

Using new String(String) can be useful if you know you need to save memory. It does this by taking a copy of the String when the original might be a substring of a much larger String. This is worth avoid as well unless you know it will save memory From Byte.decode(String)
String constant = negative ? new String("-" + nm.substring(index))
                           : nm.substring(index);
result = Byte.valueOf(constant, radix);
The first part takes a copy of the String produced even though the results of String + String is a String. It won't save any memory (as its already copy) and even if it did it is discarded after the next statement. An alternative might be
String constant = nm.substring(index);
if (negative) constant = '-' + constant;
result = Byte.valueOf(constant, radix);

Using '-' is slightly more efficient than "-" for a single character. You have to make sure you are adding it to another String or you could get a very different result. ;)

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

What does Chronicle Software do?