Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 哪个xml序列化库是面向性能的?_Java_Xml_Performance_Serialization - Fatal编程技术网

Java 哪个xml序列化库是面向性能的?

Java 哪个xml序列化库是面向性能的?,java,xml,performance,serialization,Java,Xml,Performance,Serialization,如果性能是决定因素,那么Java的最佳XML序列化库是什么 应用要点 基于Rest的API Tomcatservlet容器 需要Java对象到XML序列化 不需要反序列化或重绑定库 需要开源库 目前的业绩数字 使用“”的StringBuffer append等生成的XML。 平均响应时间=15毫秒 容易出现格式错误的XML和XML编码错误 使用XStream序列化生成的XML。 平均响应时间=200毫秒 易于维护和注释 我遇到的其他库,如JiBx、JaxB、Castor或Simp

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

应用要点

  • 基于Rest的API
  • Tomcatservlet容器
  • 需要Java对象到XML序列化
  • 不需要反序列化或重绑定库
  • 需要开源库
目前的业绩数字

  • 使用“”的StringBuffer append等生成的XML。
    • 平均响应时间=15毫秒
    • 容易出现格式错误的XML和XML编码错误
  • 使用XStream序列化生成的XML。
    • 平均响应时间=200毫秒
    • 易于维护和注释
我遇到的其他库,如JiBx、JaxB、Castor或Simple,似乎都是绑定框架,而且维护开销很大


是否还有其他高性能的XML序列化替代方案,或者我应该直接使用woodstox Stax实现(在稳定的开源库中,它似乎是最快的)使用XMLStreamWriter API实现toXml()。

我严重怀疑XStream需要200毫秒,除非你正在发送一个非常大的对象。您确定您的虚拟机已预热吗

我不会使用StringBuffer作为它的线程安全机制,在每次调用时都有一个锁。改用StringBuilder

以下测试打印

Took 56 us on average to serialise a Person
无论你是什么序列化都需要花费4000倍的时间。要么你的测试没有预热,要么你正在发送大量数据。如果是后者,我建议以二进制格式发送数据


//基于两分钟教程中的示例。
公共类XStreamTest{
公共静态类人员{
私有字符串名;
私有字符串lastname;
私人电话号码;
私人电话号码传真;
公众人物(字符串名字、字符串名字、电话号码电话、电话号码传真){
this.firstname=firstname;
this.lastname=lastname;
this.phone=电话;
this.fax=传真;
}
}
公共静态类电话号码{
私有整数码;
私有字符串编号;
公用电话号码(整数码、字符串号){
this.code=代码;
这个数字=数字;
}
}
公共静态void main(字符串…参数){
XStream XStream=新的XStream();
别名(“person”,person.class);
别名(“phonenumber”,phonenumber.class);
Person joe=新人(“joe”、“Walnes”、新电话号码(123,“1234-456”)、新电话号码(123,“9999-999”);
最终内部预热=10000;
最终整数运行=20000;
长启动=0;
for(int i=-warmup;i
// 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);
    }
}