views:

484

answers:

2

Hi,

when i serialize an array of Int32 using the BinaryFormatter, i get about 400MB/s (100 million items in one second), but when i try to serialize an array of DateTime, i get only a throughput of about 27MB/s (100 million items in 30 seconds). One DateTime occupies eight bytes in serialized form. I guess that the BinaryFormatter uses the ISerializable interface, if its implemented, so i had a look at the GetObjectData implementation of the DateTime type:

void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
    if (info == null)
    {
        throw new ArgumentNullException("info");
    }
    info.AddValue("ticks", this.InternalTicks);
    info.AddValue("dateData", this.dateData);
}

I am confused that an UInt64 and an Int64 are added to the output, which should be 16 bytes in sum, but that does not reflect my measures. So how is DateTime really serialized to binary?

A: 

It sounds like you are I/O bound on your int serialization, but you are CPU bound on your DateTime serialization (it apparently takes much more CPU time to serialize a DateTime than it does to serialize an int). Consequently, your measurements are not going to reflect an exact ratio between your two data type sizes.

Robert Harvey
Measures were done by serializing to a MemoryStream, so no I/O.
Rauhotz
OK, so you're not I/O bound, but serializing the DateTime apparently takes 30x longer than int.
Robert Harvey
A: 

It'll be more that 16 bytes, since it needs to store the keys too.

You could serialize the ticks yourself (directly)? Alternatively; I haven't tested it in this scenario, but you might give protobuf-net a whirl (it is a high performance binary serialization engine).

Marc Gravell
100 million DateTimes gives me a stream length of ~800MB, so the BinaryFormatter seems to store something like the ticks but unfortunately, quite slow.
Rauhotz
Interesting; it must have special handling... curious that it is so slow..
Marc Gravell