我严重怀疑的XStream正在200毫秒,除非你发送一个非常大的对象。你确定你的虚拟机已经被加热了吗?
我不会在每次调用时都使用StringBuffer作为其线程安全锁。改用StringBuilder。
下面的测试打印
Took 56 us on average to serialise a Person
什么都你正在连载正在4000X更长的时间。要么你的测试没有预热,要么你发送了大量的数据。如果后面是这种情况,我建议以二进制格式发送数据。
// 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);
}
}