tags:

views:

42

answers:

1

I felt xstream loading speed doesn't up to my requirement when I try to perform loading from the XML file. For an "database" with 10k ++ entries, it will take several minutes.

The following is the entire data structure I use to serialize. Size of List (symbols and codes) will be roughly 10k ++ entries.

http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/engine/StockCodeAndSymbolDatabase.java?revision=1.11&view=markup

Is there any approach I can try out, to see whether it will speed up my loading time? Able to still load back previous saved file is important as well.

The following are the code used to de-serialization. Thanks.

@SuppressWarnings("unchecked")
public static <A> A fromXML(Class c, File file) {
    XStream xStream = new XStream(new DomDriver("UTF-8"));
    InputStream inputStream = null;

    try {
        inputStream = new java.io.FileInputStream(file);
        Object object = xStream.fromXML(inputStream);
        if (c.isInstance(object)) {
            return (A)object;
        }
    }
    catch (Exception exp) {
        log.error(null, exp);
    }
    finally {
        if (false == close(inputStream)) {
        return null;
        }
        inputStream = null;
    }

    return null;
} 
A: 

Avoid using slow DomDriver.

@SuppressWarnings("unchecked")
public static <A> A fromXML(Class c, File file) {
    // Don't ever try to use DomDriver. They are VERY slow.
    XStream xStream = new XStream();
    InputStream inputStream = null;
    Reader reader = null;

    try {
        inputStream = new java.io.FileInputStream(file);
        reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        Object object = xStream.fromXML(reader);

        if (c.isInstance(object)) {
            return (A)object;
        }
    }
    catch (Exception exp) {
        log.error(null, exp);
    }
    finally {
        if (false == close(reader)) {
            return null;
        }
        if (false == close(inputStream)) {
            return null;
        }
        reader = null;
        inputStream = null;
    }

    return null;
}
Yan Cheng CHEOK