I seriously doubt that the XStream takes 200ms unless you send a very large object. Are you sure your virtual machine is hot?
I would not use StringBuffer as a safe thread with a lock on every call. Use StringBuilder instead.
The following test prints
Took 56 us on average to serialise a Person
Whatever you serialize, takes more than 4,000 times. Either your test is not warming up, or you are sending a lot of data. If in the latter case, I suggest sending the data in 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); } }
source share