Inspired from this question. I created a small benchmark program to compare ProtoBuf, BinaryFormatter and Json.NET. The benchmark itself is a small console based one at https://github.com/sidshetye/SerializersCompare .Feel free to add/improve, it's quite simple to add a new serializer to the mix. Anyway, my results are:
Binary Formatter ProtoBuf Json.NET ServiceStackJson ServiceStackJSV
Loop Size:512 bytes Size:99 bytes Size:205 bytes Size:205 bytes Size:181 bytes
1 16.1242 ms 151.6354 ms 277.2085 ms 129.8321 ms 146.3547 ms
2 0.0673 ms 0.0349 ms 0.0727 ms 0.0343 ms 0.0370 ms
4 0.0292 ms 0.0085 ms 0.0303 ms 0.0145 ms 0.0148 ms
8 0.0255 ms 0.0069 ms 0.0017 ms 0.0216 ms 0.0129 ms
16 0.0011 ms 0.0064 ms 0.0282 ms 0.0114 ms 0.0120 ms
32 0.0164 ms 0.0061 ms 0.0334 ms 0.0112 ms 0.0120 ms
64 0.0347 ms 0.0073 ms 0.0296 ms 0.0121 ms 0.0013 ms
128 0.0312 ms 0.0058 ms 0.0266 ms 0.0062 ms 0.0117 ms
256 0.0256 ms 0.0097 ms 0.0448 ms 0.0087 ms 0.0116 ms
512 0.0261 ms 0.0058 ms 0.0307 ms 0.0127 ms 0.0116 ms
1024 0.0258 ms 0.0057 ms 0.0309 ms 0.0113 ms 0.0122 ms
2048 0.0257 ms 0.0059 ms 0.0297 ms 0.0125 ms 0.0121 ms
4096 0.0247 ms 0.0060 ms 0.0290 ms 0.0119 ms 0.0120 ms
8192 0.0247 ms 0.0060 ms 0.0286 ms 0.0115 ms 0.0121 ms
Disclaimer:
The results above are from within a Windows VM - the Stopwatch/timer values for very small intervals may not be 100% accurate compared to bare-metal OSes. So ignore ultra low values in the above table.
For ServiceStack, the Json and JSV scored were taken from two separate runs. Since they share the same underlying ServiceStack library, running one right after the other affects the "cold start" 1 loop scores for the next run (it's 'warm start' fast)
BinaryFormatter is the largest in size but also the fastest for a single serialization => deserialization loop. However, once we tight loop around the serialization => deserialization code, ProtoBuf is super fast.
Question#1: Why is ProtoBuf that much slower for a single serialization => deserialization loop?
Question#2: From a practical perspective, what can we do to get past that "cold start"? Run at least one object (of any type) through it? Run every (critical) object type through it?