0

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

    }
}
deltanovember
  • 42,611
  • 64
  • 162
  • 244

4 Answers4

2

Try to run the two in reverse. First the one million then single and see what happens.

Mr1159pm
  • 774
  • 1
  • 10
  • 21
2

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 Pojo class 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.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
0

Just FYI, your second time should actually be 79ms

The code should read:

System.out.println(end1+ " " + end2);
James Bassett
  • 9,458
  • 4
  • 35
  • 68
0

Create a new 'pojo' object to send each time. You might also want to have different values for 'blah' in each instance of pojo. e.g. "" + Math.random().

I think you'll see the performance for the one million be more like you expect. IIRC when you serialize the same object again and again to the same stream, the Java libraries will cache it in a dictionary and only send a 'reference' back to the originally sent object. So that's why you're seeing such a crazy fast number for the million pojo stream.

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57