优化xstream的加载速度 [英] Optimize loading speed of xstream

查看:211
本文介绍了优化xstream的加载速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从XML文件执行加载时,我觉得xstream加载速度达不到我的要求。对于
一个包含10k ++条目的数据库,它将花费几分钟。

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.

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

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


推荐答案

避免使用慢速DomDriver。

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

这篇关于优化xstream的加载速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆