Below is my benchmarking code. First I try to serialize a single object. Then I try to do the same thing one million times. I was expecting a proportional increase in timing. Instead I get
1323048944117 1323048944131
1323048944117 1323048944210
14ms for one object
93ms for 1mil objects
What is happening here? I’m most worried about the time for single object conversion which I’m hoping to get to the submillisecond level.
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Pojo implements Serializable {
String blah = "11111111111111111111aaaaaaaaaaaaaaaaaaaa";
}
public class SerializeTest {
public static void main(String[] args) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
Pojo pojo = new Pojo();
long start = 0;
long end1 = 0;
try {
out = new ObjectOutputStream(bos);
start = System.currentTimeMillis();
out.writeObject(pojo);
end1 = System.currentTimeMillis();
for (int i=0;i<1000000;i++) {
out.writeObject(pojo);
}
} catch (Exception ex) {
}
long end2 = System.currentTimeMillis();
System.out.println(start + " " + end1);
System.out.println(start + " " + end2);
}
}
Ultimately @duffmo is right that your results are 98% about the problems with doing micro testing in Java than it is about serialization. He posted a good link to read.
Another factor that is confusing your test is that serialization is smart. Writing the first object writes all of the class and field information and the data. The 2nd to the nth write of the same object instance is only writing a reference to the first object. The first object looks to be ~120 bytes and each remaining one is only 5 bytes. It’s also going to be tons faster because it does a lot less.
My test did the 1st object in 8ms, the next 1000 objects in 5ms, and then a million objects in 2000ms. This shows you mostly how fast serialization is. It also shows you that Java does some magic real-time optimizations that cause drastically non-linear speed graphs.
If you are trying to do some serialization speed testing I would make your
Pojoclass generate some random numbers (or a random string) and I would compare the serialization of 1000 objects to a million to have a better comparison of per-object serialization times. Either that or start the timing after you have written 1000 objects or something. For example, writing a different object produces a file that is ~10 times larger but only took 5600ms.