Windows Phone 7 Serialization: Comparison
Join the DZone community and get the full member experience.
Join For FreeIn this post I am going to compare several ways of serialization in Windows Phone 7 applications. I will show how much time was needed to serialize and deserialize an object and what size did the output stream had. Comparison includes Xml Serialization, DataContract Serialization, DataContract JSON Serialization and my Binary Serialization for Windows Phone 7, Json.NET Serialization, sharpSerializer. I have described each of those types in my previous posts.
Tests where made using Windows Phone 7 device by LG.
Introduction
Before comparing several types of Windows Phone 7 (and Silverlight ) serialization I would like to say that each type has its own advantages and disadvantages for a certain types of tasks. That means that for storing objects in Isolated Storage one type is better than another, but when it comes to Windows Communication Foundation (WCF) then it could be vice versa. So first of all you need to define your task. As for my comparison, my mission was to find the most suitable way of serialization for storing objects in Isolated Storage. Due to the fact that Windows Phone 7 devices have a very limited amount of memory file (stream) size was an important criteria in my tests.
Sample Object
First, I have created a sample class that is supported by each serialization type mentioned in this post. Here is a code for that class:
[DataContract] public class SampleData { [XmlElement] [DataMember] public string ContentText { get; set; } [XmlElement] [DataMember] public List<int> SomeItems { get; set; } public void Fill() { StringBuilder sb = new StringBuilder(); for (int index = 0; index < 1000; index++) { sb.Append("sometext sometext\n"); } ContentText = sb.ToString(); SomeItems = new List<int>(); for (int index = 0; index < 1000; index++) { SomeItems.Add(index); } } }
Testing XML Serialization
I have begun my journey with XML Serialization using XmlSerializer. I have created a method for testing (look at the code bellow) and run that method exactly five times. Stream object length was 48070 bytes.
public void TestXMLSerializer(SampleData sd) { using (MemoryStream ms = new MemoryStream()) { // time in milliseconds double serTime = TestMethod(() => XMLSerializerHelper.Serialize(ms, sd)); long size = ms.Length; ms.Position = 0; double deSerTime = TestMethod(() => XMLSerializerHelper.Deserialize(ms, typeof(SampleData))); } }
You can see the results for XML Serialization for Windows Phone 7
bellow. This type of serialization is pretty fast, but the stream size
is quite huge.
Type | Time (in milliseconds) | Average | ||||
Serialization | 309 | 309 | 306 | 310 | 304 | 307.6 |
Deserialization | 191 | 188 | 187 | 189 | 193 | 189.6 |
Testing DataContract Serialization
Next I have tested DataContract Serialization. Once again I have created a method for testing (code bellow). Stream length was 42165 bytes.
public void TestDataContractSerialization(SampleData sd) { using (MemoryStream ms = new MemoryStream()) { // time in milliseconds double serTime = TestMethod(() => DataContractSerializationHelper.Serialize(ms, sd)); long size = ms.Length; ms.Position = 0; double deSerTime = TestMethod(() => DataContractSerializationHelper.Deserialize(ms, typeof(SampleData))); } }
Take a look at DataContract Serialization results in a table bellow.
Probably the one of the worst results in this comparison. Stream size is
slightly better than the stream size during XML Serialization.
Type | Time (in milliseconds) | Average | ||||
Serialization | 469 | 471 | 469 | 478 | 471 | 471.6 |
Deserialization | 190 | 196 | 198 | 200 | 193 | 195.4 |
Testing DataContract JSON Serialization
Another competitor was DataContract JSON Serialization. Method for testing is shown bellow. Stream length was 22922 bytes.
public void TestDataContractJSONSerialization(SampleData sd) { using (MemoryStream ms = new MemoryStream()) { // time in milliseconds double serTime = TestMethod(() => DataContractJSONSerializationHelper.Serialize(ms, sd)); long size = ms.Length; ms.Position = 0; double deSerTime = TestMethod(() => DataContractJSONSerializationHelper.Deserialize(ms, typeof(SampleData))); } }
The results for DataContract JSON Serialization (table bellow) are
better than for DataContract Serialization, especially comparing the
stream size.
Type | Time (in milliseconds) | Average | ||||
Serialization | 494 | 494 | 492 | 490 | 497 | 493.4 |
Deserialization | 189 | 188 | 197 | 188 | 189 | 190.2 |
Testing custom Binary Serialization
Finally, my custom Binary Serialization for Windows Phone 7 was showing the best results in that comparison. Check the method I’ve used for testing bellow. Stream length was 22007 bytes.
public void TestBinarySerialization(SampleData sd) { using (MemoryStream ms = new MemoryStream()) { // time in milliseconds double serTime = TestMethod(() => BinarySerializationHelper.Serialize(ms, sd)); long size = ms.Length; ms.Position = 0; double deSerTime = TestMethod(() => BinarySerializationHelper.Deserialize(ms, typeof(SampleData))); } }
Here are the results I’ve received for my custom Binary
Serialization. Deserilization time is astonishing. Also the stream size
is less than in DataContract JSON Serialization.
Type | Time (in milliseconds) | Average | ||||
Serialization | 123 | 130 | 123 | 124 | 123 | 124.6 |
Deserialization | 21 | 22 | 21 | 21 | 22 | 21.4 |
Testing Json.NET Serialization
After several comments regarding Json.NET I have decided to add it to my comparison. I have also created a tutorial describing how to serialize and deserialize objects using Json.NET approach. Stream length was not that huge as for xml-based serializations, only 22922 bytes (similar to DataContract JSON serialization stream size).
public void TestJSONNETSerialization(SampleData sd) { using (MemoryStream ms = new MemoryStream()) { // time in milliseconds double serTime = TestMethod(() => JSONNETSerializationHelper.Serialize(ms, sd)); long size = ms.Length; ms.Position = 0; double deSerTime = TestMethod(() => { JSONNETSerializationHelper.Deserialize(ms, typeof(SampleData)); }); } }
Results (table bellow) are slightly better comparing to DataContract
and DataContractJSON serialization types, but there is no chance to beat
the results of custom Binary serialization.
Type | Time (in milliseconds) | Average | ||||
Serialization | 451 | 421 | 411 | 415 | 420 | 423.6 |
Deserialization | 133 | 132 | 131 | 137 | 131 | 132.8 |
Testing sharpSerializer
Personally, I like this csharpSerializer a lot and the test results for this serialization look quite promising too. Stream size for csharpSerializer Binary serialization was 28177 bytes and for csharpSerializer XML serialization it was 54292 bytes.
public void TestSharpSerializer(SampleData sd) { using (MemoryStream ms = new MemoryStream()) { // time in milliseconds double serTime = TestMethod(() => SharpSerializerHelper.Serialize(ms, sd)); long size = ms.Length; ms.Position = 0; double deSerTime = TestMethod(() => { SharpSerializerHelper.Deserialize(ms); }); } }
I have tested both types of sharpSerializer XML and Binary. Binary serialization results are quite nice, XML-output results are almost the same as for XMLSerializer.
Binary (sharpSerializer)
Type | Time (in milliseconds) | Average | ||||
Serialization | 250 | 250 | 247 | 247 | 249 | 248.6 |
Deserialization | 60 | 60 | 61 | 61 | 60 | 60.4 |
XML (sharpSerializer)
Type | Time (in milliseconds) | Average | ||||
Serialization | 278 | 283 | 280 | 286 | 278 | 281 |
Deserialization | 179 | 177 | 179 | 185 | 176 | 179.2 |
Final results
Bellow is a table of final results to compare those four ways of serialization for Windows Phone 7 (and, perhaps, Silverlight). According to those results we have a pure winner: Binary Serialization. Probably, my implementation of Binary Serialization is not yet perfect, but still it is showing the best results in my comparison and tests.
Type | Serialization (ms) | Deserialization (ms) | Size (byte) | |
Custom Binary Serialization | 124.6 | 21.4 | 22007 | |
sharpSerializer Binary Serialization | 248.6 | 60.4 | 28177 | |
sharpSerializer XML Serialization | 281 | 179.2 | 54292 | |
XMLSerializer | 307.6 | 189.6 | 48070 | |
Json.NET Serialization | 423.6 | 132.8 | 22922 | |
DataContract JSON Serialization | 493.4 | 190.2 | 22922 | |
DataContract Serialization | 471.6 | 195.4 | 42165 | |
Printable version
Summary
As I have said earlier there are many specific tasks in development
and each of those tasks requires a specific approach. From my point of
view, if the task is in storing serialized data in Isolated Storage then
I would use sharpSerializer (Binary) or my custom Binary Serialization,
because the size of serialized data is small. Also the speed of
serialization and deserialization is quite nice. When your task requires
to transfer data over network (for example) then I would select a type
among XML-based serializations or JSON-based serializations. One
important thing is that test results generated by Windows Phone 7
Emulator and a real Windows Phone 7 Device differ a lot
Source code
You can try running those tests on your own custom objects. Here is a source code for that:
SOURCE CODE
Source: http://www.eugenedotnet.com/2010/12/windows-phone-7-serialization-comparison/
Opinions expressed by DZone contributors are their own.
Comments