[jdom-interest] Test results for JDOM w/o exceptions

Petri Wessman orava at termiitti.akumiitti.fi
Mon Jul 10 08:52:39 PDT 2000

Ok, I went ahead and hacked up a getAttribute for JDOM (Element.java)
that returns null instead of an exception, as follows:

    public Attribute getAttributeHacked(String name, String uri) {

        Iterator i = attributes.iterator();
        while (i.hasNext()) {
            Attribute att = (Attribute)i.next();
            if ((att.getNamespaceURI().equals(uri)) &&
                (att.getName().equals(name))) {
                return att;

        // If we got here, nothing found
	return null;

I then proceeded to run a test with it, iterating 1M times over an
attribute fetch for an attribute that does not exist using the current
getAttribute, and then another 1M times using the hacked version.

The differences were dramatic. So dramatic in fact that I may have
done something wrong/stupid, I've enclosed the test code below. Of
course, this example demonstrates an extreme case in which the
attribute searched for is never found, but since it's quite usual for
optinal attributes to be missing at least 50% of the time, these
results speak quite well against using exceptions in the normal flow
of things.

It seems that the Java design guideline "don't throw exceptions for
normal situations" is also a very good performace guideline.

Results on a Debian Linux PentiumIII/550 machine:

JVM				JDOM CVS snapshot	 Hacked JDOM

Blackdown 1.2.2.RC2 (JIT)	82s			  5s
IBM 1.3 preview	(JIT)		25s			  1s
Sun 1.2.2 (no JIT)		75s			 13s


-------------- next part --------------
A non-text attachment was scrubbed...
Name: JDOMTimeTest.java
Type: application/octet-stream
Size: 2131 bytes
Desc: test java class
Url : http://jdom.org/pipermail/jdom-interest/attachments/20000710/2faa626e/JDOMTimeTest.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sitemap.xml
Type: application/octet-stream
Size: 257 bytes
Desc: test XML
Url : http://jdom.org/pipermail/jdom-interest/attachments/20000710/2faa626e/sitemap.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sitemap.dtd
Type: application/octet-stream
Size: 458 bytes
Desc: test DTD
Url : http://jdom.org/pipermail/jdom-interest/attachments/20000710/2faa626e/sitemap-0001.obj

More information about the jdom-interest mailing list