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

Steve Weiss sweiss at aamc.org
Mon Jul 10 10:33:21 PDT 2000


Maybe I'm taking an overly simplistic view of things, but doesn't this
make sense? Instantiating an object, as in 'throw new FooException()',
does incur overhead, no? As opposed to 'return null;'?

-S

Petri Wessman wrote:
> 
> 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
> 
> //Petri
> 
>   ------------------------------------------------------------------------
>                            Name: JDOMTimeTest.java
>    JDOMTimeTest.java       Type: unspecified type (application/octet-stream)
>                        Encoding: base64
>                     Description: test java class
> 
>                      Name: sitemap.xml
>    sitemap.xml       Type: unspecified type (application/octet-stream)
>                  Encoding: base64
>               Description: test XML
> 
>                      Name: sitemap.dtd
>    sitemap.dtd       Type: unspecified type (application/octet-stream)
>                  Encoding: base64
>               Description: test DTD

-- 
Steve Weiss      Association of American Medical Colleges
(202)828-0428    mailto:sweiss at aamc.org    http://www.aamc.org



More information about the jdom-interest mailing list