| Sign In/My Account | View Cart |
Faster List Iteration with RandomAccess Interface
Pages: 1, 2
Times shown are the average of three runs, and all times have been normalized
to the first table cell; i.e., the time taken by the ArrayList to iterate the list using the List.get() method, using java -client.
Table 1: Access times for loop types and access methods
| Loop type (loop test) and access method | ArrayList java -client |
LinkedList java -client |
ArrayList java -server |
LinkedList java -server |
| loop counter (i<n) and |
100% | too long | 77.5% | too long |
| iterator |
141% | 219% | 109% | 213% |
| iterator (i<n) and | 121% | 205% | 98% | 193% |
| RandomAccess test with loop from |
100% | 205% | 77.5% | 193% |
Note that HotSpot is capable of optimizing away accesses that are unnecessary, so the test accessed and operated on the list elements in a way that could not eliminate the list element access. The test code is available here.
The most important results are in the last two rows of the table. The last row shows the
times obtained by making full use of the RandomAccess interface;
the row before that shows the most optimal general technique for iterating
lists, if RandomAccess were not available. The size of the lists I
used for the test (and consequently, the number of loop iterations required to
access every element) was sufficiently large that the instanceof
test had no measurable cost in comparison to the time taken to run the loop.
Consequently, we can see that that there was no cost (but also no benefit) in
adding the instanceof RandomAccess test when iterating
the LinkedList; whereas the ArrayList was iterated
more than 20% quicker when the instanceof test was included.
What should you do if you are implementing code now? Obviously, you can start
developing with a 1.4 (beta) release, but this is not an option everywhere.
There are three aspects to using RandomAccess if you are developing
code now:
RandomAccess without moving to 1.4; many development environments
cannot be upgraded rapidly or to a beta release.RandomAccess does not exist.RandomAccess when running in
a 1.4+ JVM. Making RandomAccess available to your development environment is
the first issue, and this can be as simple as adding the
RandomAccess interface to your classpath. Any version of the SDK
can create the RandomAccess interface. The definition for
RandomAccess is
package java.util;
public interface RandomAccess {}
This interface can be created using javac, as follows:
temptemp, create a directory called javajava, create a directory called utilutil, create a file called RandomAccess.java, containing
the definition just givenRandomAccess.java, using javac: javac
RandomAccess.javaNow including temp in your classpath should enable classes that refer
to RandomAccess to be compiled.
Some Java integrated development environments (IDEs) can make it difficult to
add a class to the core SDK packages. If this is the case for your IDE, your
only hope is that it accepts an external classpath for compilation purposes, in
which case the custom-generated RandomAccess class will need to be
held in that external classpath.
We also need to handle RandomAccess in the runtime environment.
For pre-1.4 environments, the test if (listObject instanceof
RandomAccess) will generate a NoClassDefFoundError at
runtime, when the JVM tries to load the RandomAccess class. For the
instanceof test to be evaluated, the class has to be loaded;
however, we can guard the test so that it is only executed if
RandomAccess is available. The simplest way to do this is to check
if RandomAccess exists, setting a boolean guard as the outcome of
that test:
static boolean RandomAccessExists;
..
//execute this as early as possible after the
//application starts
try
{
Class c = Class.forName("java.util.RandomAccess");
RandomAccessExists = true;
}
catch (ClassNotFoundException e)
{
RandomAccessExists = false;
}
Then, finally, we will need to change our instanceof tests to
use the RandomAccessExists variable as a guard:
if (RandomAccessExists && (listObject instanceof RandomAccess) )
Now we have the solution for all three aspects mentioned at the beginning of this section:
RandomAccess interface can be created and compiled easily
with any SDK, and this manually-compiled version can be used at compilation
time as a stand-in, to compile any code which refers to
RandomAccess.instanceof test will automatically revert to the
Iterator loop if RandomAccess does not exist, and should avoid
throwing a NoClassDefFoundError in pre-1.4 JVMs.
instanceof test will also automatically use
the faster loop branch when RandomAccess does exist and the list
object implements it.Java Performance Tuning by Jack Shirazi (O'Reilly, 2000)
Source code for the tests used in this article
Jack Shirazi is the author of Java Performance Tuning. He was an early adopter of Java, and for the last few years has consulted mainly for the financial sector, focusing on Java performance.
Read more Java Design and Performance Optimization columns.
Return to ONJava.com.