如果性能是决定因素,那么Java的最佳XML序列化库是什么?
显着的应用点
当前业绩编号
我遇到的其他库如JiBx,JaxB,Castor或Simple似乎是绑定框架,似乎有很大的维护开销。
是否还有其他高性能的XML序列化替代品,或者我应该继续使用使用woodstox Stax实现的XMLStreamWriter API实现toXml()(它似乎有报告称为此目的中稳定的开源库中最快)?
答案 0 :(得分:2)
我非常怀疑XStream需要200毫秒,除非你发送一个非常大的对象。你确定你的VM已经预热了吗?
我不会使用StringBuffer作为其线程安全,并且每次调用时都会锁定。请改用StringBuilder。
以下测试打印
Took 56 us on average to serialise a Person
你串行化的时间要长4000倍。要么您的测试没有预热,要么您正在发送大量数据。如果是后者,我建议以二进制格式发送数据。
// 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);
}
}
答案 1 :(得分:1)