Wednesday, March 3, 2010

Java(tm) charAt()


http://www.glenmccl.com/perfj_008.htm

Suppose that you have a need to find a character in a string, that is, return a position >= 0 of where a character occurs, or -1 if not found. In a language like C, with its pointers and low-level approach to programming, it's often just as efficient or more so to code a loop for finding a character directly, instead of using a library function like strchr(). Especially with short strings, avoiding the function call overhead is often worth a lot.

Is this true of Java? Suppose that we code up a similar example:

        public class index {
public static void main(String args[])
{
String s = "aaaaaaaaaa";
int i = 250000;
int n = 0;

// method #1

if (args[0].compareTo("index") == 0) {
while (i-- > 0)
n = s.indexOf('x');
}

// method #2

else {
while (i-- > 0) {
int len = s.length();
n = -1;
for (int j = 0; j < len; j++) {
if (s.charAt(j) == 'x') {
n = j;
break;
}
}
}
}
}
}

When we run this code with JDK 1.0, we find that method #1 is about 3X as fast as method #2, which is the equivalent of using pointers in C. Why is this? There are a couple of reasons. One is that charAt() is a method call, and not merely a quick peek that retrieves a character at a given position.

The other reason for the slower performance is that charAt() checks the index to ensure that it's within bounds. So, even though we're iterating over the characters of a String in a safe way (0 to s.length()-1), the checks are done anyway.

Because of the method call overhead and the index checking, indexOf() wins easily. It avoids the method call overhead, and the index checking is not done but instead characters are accessed directly from the internal char[] vector that underlies a String.

It's probably a little early to say how an area like this one will shake out. charAt() could conceivably be expanded as an inline, though it's not declared as a final method in JDK 1.0. And the subscript checking is likely to be left in place, because it's part of what Java guarantees to the programmer.




Our Services

Back to Home Page


Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Throughout this document, when the Java trademark appears alone it is a reference to the Java programming language. When the JDK trademark appears alone it is a reference to the Java Development Kit.

No comments:

Post a Comment