péntek, november 27th, 2009 | Author:

A post in English, because I believe there are a lot of people out there who can use this “fix”.

Let’s start with an example: You have a moderately large library. You have different sections, each section has some subsections and each subsection has a handful of books in it.
You want to make a catalogue from your books. Today is the day when you will process the books in the nature section, animals subsection. You want to get the name of the author, the title and the number of pages in each books.
You work hard, and in five hours, you are finished with all of the 81 books in that section.

Your manager is not satisfied with this, so he thinks for a while, and tell you to change the process. There is one additional step to make: you need to make copy of the whole book, collect the required data from it and then you trash it. He insists that you must do this, so you start working again. As an experiment, you create the same catalogue from the same 81 books.
How long did it take? Let’s see..

In programming terms, the first solution looks like this:

NodeList nl = (NodeList) bookPath.evaluate(doc, XPathConstants.NODESET);

        for(int i = 0;i<nl.getLength();i++) {

            Node n = nl.item(i);

            String str = (String)title.evaluate(n, XPathConstants.STRING);

            str = (String)author.evaluate(n, XPathConstants.STRING);

            str = (String)numberOfPages.evaluate(n, XPathConstants.STRING);

        }

Running this piece of code took 5406ms alltogether. 5 seconds seems to be a bit long, so let’s put in some copying:

        NodeList nl = (NodeList) bookPath.evaluate(doc, XPathConstants.NODESET);

        for(int i = 0;i<nl.getLength();i++) {

            Node n = nl.item(i);

            Node n2 = n.cloneNode(true);

            String str = (String)title.evaluate(n2, XPathConstants.STRING);

            str = (String)author.evaluate(n2, XPathConstants.STRING);

            str = (String)numberOfPages.evaluate(n2, XPathConstants.STRING);

        }

Running this piece (remember, there is one extra step in it for each and every one of the “book” nodes) took: 688ms. It is 8 times faster.

I’ve run this example on a 200kB XML using the built-in XPath library of Java 5 (which is to my best knowledge Xalan).

Something is definitely wrong here. I’ve tried to find out on Google what others think about this, and it seems that there are a lot of people who were running into this problem. I hope it will be fixed in the close future, because this workaround is not at all obvious.

Tags: ,
Category: Hobbi, Szakmai
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply » Regisztráció / Log in


You must be logged in to post a comment.