Some interesting results for serializing a long array:
2009-10-19 15:38:44,389 [main] INFO foo - COMPARE SERIALIZATION
2009-10-19 15:38:51,062 [main] INFO foo - gson serialization (ns):
66421766
2009-10-19 15:38:51,324 [main] INFO foo - jackson serialization
(ns): 1213741
so, 66ms for gson, 1.2ms for jackson. see the code below.
Interestingly, removing the @Expose annotations (which are not used
for this case anyway), yields a big change:
2009-10-19 15:42:25,299 [main] INFO foo - COMPARE SERIALIZATION
2009-10-19 15:42:28,177 [main] INFO foo - gson serialization (ns):
28148362
2009-10-19 15:42:28,444 [main] INFO foo - jackson serialization
(ns): 1664830
This is on a core 2 duo 2.8GHz ubuntu 9.04 (2.6.28), hotspot 14.2 jvm
1.6.0_16.
I'd be curious if anyone else can duplicate these results.
public class PerformanceTest {
public static class SampleClass {
@Expose
public String foo;
@Expose
public List<AnotherSampleClass> bar = new
ArrayList<AnotherSampleClass>();
}
public static class AnotherSampleClass {
@Expose
public String foo;
public AnotherSampleClass(String foo) {
this.foo = foo;
}
}
@Test
public void gsonVsJackson() throws JsonGenerationException,
JsonMappingException, IOException {
Logger.getLogger("foo").info("COMPARE SERIALIZATION");
SampleClass c = new SampleClass();
c.foo = "hi";
c.bar.add(new AnotherSampleClass("hi"));
for (int i = 0; i < 5000; ++i) {
c.bar.add(new AnotherSampleClass("hi"));
}
int iterations = 100;
{
Gson g = new GsonBuilder().create();
StringWriter w = new StringWriter();
long t1 = System.nanoTime();
for (int i = 0; i < iterations; ++i) {
g.toJson(c, w);
}
long t2 = System.nanoTime();
Logger.getLogger("foo").info("gson serialization (ns): " +
String.valueOf( (t2 - t1) / iterations));
}
{
ObjectMapper m = new ObjectMapper();
StringWriter w = new StringWriter();
long t1 = System.nanoTime();
for (int i = 0; i < iterations; ++i) {
m.writeValue(w, c);
}
long t2 = System.nanoTime();
Logger.getLogger("foo").info("jackson serialization (ns):
" + String.valueOf( (t2 - t1) / iterations));