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);
}
}