[jdom-interest] RE: Reference comparisons in SAXHandler.startElement

GB/DEV - Philip Nelson philip.nelson at omniresources.com
Thu Mar 15 06:43:39 PST 2001

> Is it about trying to squeeze some performance out of
> SAXHandler.startElement by changing a few .equals to == ?

Jason already posted his interest in intern is for memory savings.

My interest in this thread was more because of profiling some of my own JDOM
code and seeing that .equals was the number one thing that JDOM did.  I was
interested in the comments like "if the strings are different lengths it
will be faster" or "if the strings start with different characters it will
be faster"
> If so, I have a hard time imaging that that 
> micro-optimization can have much
> impact because there's so much other stuff going on in
> SAXHandler.startElement.  (Well, maybe it could have an impact if the
> strings are fairly long and have common prefixes.)

It would appear you are right (as are others who have spoken up on this).  I
am just glad we're talking about it ;-)  JDOM is supposed to be fast and
lightweight but seems to be neither at this point.  We have taken the
reasonable approach of get it working first and then optimize it.  OK, but
the release date on the JSR page is in August and so it would seem to be
time to start making it fast and light ;^)

> (When you post test results, can you please include the JVM 
> version and
> other platform information?  The actual test code would be 
> nice, too, but
> that might be too much trouble.  It's just that tests are so 
> variable...)

Right, and I am *not* an expert in writing performance tests.

In what I wrote for these .equals() vs == test was as simple as I could make
Sun JVM 1.2.2, Windows NT 4 WS, 256MB ram (cpu shouldn't matter because
there were only relative times)

Here is the code sans silly generated comments.  I used static methods to
make it easy to identify the test in the profiler.  The timings of each
method call were measured and compared.  Different orders made no
difference. FWIW, interning any of the strings also made no difference.

class SimpleTest {
	 * Starts the application.
	 * @param args an array of command-line arguments
	public static void main(java.lang.String[] args) {
		// Insert code to start the application here.

		String one= "yyyyy";
		String two= "yyyyy";
		String three= "yyyyy";
		String four= "xyyyy";
		String five= "1";
		String six= "11";

		testEquals(three, four);
		testEE(three, four);
		testEquals(five, six);
		testEE(five, six);
		testEquals(one, two);
		testEE(one, two);


	public static void testEE(String a, String b) {
		boolean x;
		int i= 0;
		while (i < 10000) {
			x= a == b;

	public static void testEquals(String a, String b) {

		boolean x;
		int i= 0;
		while (i < 10000) {
			x= a.equals(b);


More information about the jdom-interest mailing list