哪个xml序列化库是面向性能的? [英] Which xml serialization library is performance oriented?

查看:70
本文介绍了哪个xml序列化库是面向性能的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果性能是决定性因素,那么最好的 Java XML 序列化库是什么?

What is the best XML serialization library for Java if performance is the deciding factor?

应用要点

  • 基于休息的 API.
  • Tomcat Servlet 容器
  • 需要 Java 对象到 XML 序列化
  • 不需要反序列化或重绑定库.
  • 需要开源库.

当前性能数据

  • 使用 StringBuffer 附加<"、>"等生成的 XML.
    • 平均响应时间 = 15 毫秒.
    • 容易出现格式错误的 XML 和 xml 编码错误.
    • 平均响应时间 = 200 毫秒.
    • 易于维护和注释.

    我遇到的其他库,例如 JiBx、JaxB、Castor 或 Simple 似乎是绑定框架,并且似乎有大量的维护开销.

    The other libraries which I've come across such as JiBx, JaxB, Castor or Simple seem to be binding frameworks and seem to have a heavy maintenance overhead.

    是否有其他高性能的 XML 序列化替代方案,或者我应该继续使用 XMLStreamWriter API 使用 woodstox Stax 实现来实现 toXml()(这似乎有报告说它是稳定开源库中最快的)?

    Are there other high performant alternatives for XML serialization or should I just go ahead and implement toXml() using XMLStreamWriter API using woodstox Stax implementation(which seems to have reports of being the fastest among stable open source libraries for the purpose)?

    推荐答案

    我严重怀疑 XStream 是否需要 200 毫秒,除非您要发送一个非常大的对象.您确定您的 VM 已预热吗?

    I seriously doubt XStream is taking 200 ms, unless you are sending a very large object. Are you sure your VM is warmed up?

    我不会将 StringBuffer 用作它的线程安全,并在每次调用时锁定.改用 StringBuilder.

    I wouldn't use StringBuffer as its thread safe with a lock on every call. Use StringBuilder instead.

    以下测试打印

    Took 56 us on average to serialise a Person
    

    您正在连载的内容需要花费 4000 倍的时间.要么您的测试没有预热,要么您发送了大量数据.如果是后者,我建议以二进制格式发送数据.

    What ever you are serialising is taking 4000x longer. Either your test is not warmed up or you are sending alot of data. If the later is the case I suggest sending data in a binary format.

    // based on the example in the two-minute tutorial.
    public class XStreamTest {
        public static class Person {
            private String firstname;
            private String lastname;
            private PhoneNumber phone;
            private PhoneNumber fax;
    
            public Person(String firstname, String lastname, PhoneNumber phone, PhoneNumber fax) {
                this.firstname = firstname;
                this.lastname = lastname;
                this.phone = phone;
                this.fax = fax;
            }
        }
    
        public static class PhoneNumber {
            private int code;
            private String number;
    
            public PhoneNumber(int code, String number) {
                this.code = code;
                this.number = number;
            }
        }
    
        public static void main(String... args) {
            XStream xstream = new XStream();
            xstream.alias("person", Person.class);
            xstream.alias("phonenumber", PhoneNumber.class);
    
            Person joe = new Person("Joe", "Walnes", new PhoneNumber(123, "1234-456"), new PhoneNumber(123, "9999-999"));
    
            final int warmup = 10000;
            final int runs = 20000;
    
            long start = 0;
            for (int i = -warmup; i < runs; i++) {
                if(i == 0) start = System.nanoTime();
                String xml = xstream.toXML(joe);
            }
            long time = System.nanoTime() - start;
            System.out.printf("Took %,d us on average to serialise a Person%n", time / runs / 1000);
        }
    }
    

    这篇关于哪个xml序列化库是面向性能的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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