优化xstream的加载速度

发布于 2024-09-17 07:33:14 字数 1178 浏览 7 评论 0原文

当我尝试从 XML 文件执行加载时,我感觉 xstream 加载速度达不到我的要求。为了 一个有 10k ++ 条目的“数据库”,需要几分钟。

以下是我用来序列化的整个数据结构。列表的大小(符号和代码)将为 大约 10k ++ 条目。

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

有吗我可以尝试一下这种方法,看看它是否会加快我的加载时间?仍然可以加载回来 以前保存的文件也很重要。

以下是用于反序列化的代码。谢谢。

@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;
} 

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;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2024-09-24 07:33:14

避免使用速度慢的 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文