commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Varszegi <>
Subject [lang] Performance enhancement for SerializationUtils
Date Sat, 16 Nov 2002 23:39:50 GMT
Here is the code for the serialize(Serializable) method:

    public static byte[] serialize(Serializable obj) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serialize(obj, baos);
        return baos.toByteArray();

This can be sped up a significant amount just by using a different constructor for the
ByteArrayInputStream instance, passing in a number higher than 32, so it constructs a bigger
initial buffer than it otherwise would.  Bumping up the initial buffer size to 128 worked
about as
well as bumping it up to 512 on my machine.

This alone resulted in an overall 6% speed increase for the clone(Serializable) method on

For the methods serialize(Serializable, OutputStream) and deserialize(InputStream) it may
desirable to wrap the passed streams in buffered streams.  Serialization to/from streams is
typically done for persistence, passing objects between JVMs, etc. and it's not hard to imagine
someone passing in unbuffered streams in these situations...


Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message