Java Reference
In-Depth Information
So far, the example saves about 15% of the total time to serialize and deserialize the object.
But this test has not actually re-created the histogram object on the receiving side: that ob-
ject will be created when the receiving code first accesses it.
There are times that the histogram object will not be needed: the client may only be interes-
ted in the prices on particular days, and not the histogram. That is where the more unusual
case comes in: if the histogram will always be needed, and if it takes more than 3.1 seconds
to calculate all the histograms in this test, then the case with the lazily initialized fields will
actually have a net performance decrease.
In this case, calculating the histogram does not fall into that category—it is a very fast opera-
tion. In general, it may be hard to find a case where recalculating a piece of data is more ex-
pensive than serializing and deserializing that data. But it is something to consider as code is
This test is not actually transmitting data; the data is written to and read from preallocated
byte arrays, so that it measures only the time for serialization and deserialization. Still, notice
that making the histogram field transient has also saved about 13% in the size of the data.
That will be quite important if the data is to be transmitted via a network.
Compressing Serialized Data
This leads to a third way that serialization performance of code can be improved: compress
the serialized data so that it is faster to transmit over a slow network. In the stock history
class, that is done by compressing the prices map during serialization:
public class
class StockPriceHistoryCompress
implements StockPriceHistory , Serializable {
private byte
byte [] zippedPrices ;
private transient
transient SortedMap < Date , StockPrice > prices ;
private void
void writeObject ( ObjectOutputStream out )
throws IOException {
iif ( zippedPrices == null
null ) {
makeZippedPrices ();
out . defaultWriteObject ();
private void
void readObject ( ObjectInputStream in )
Search WWH ::

Custom Search