I am serializing a generic dictionary in VB.net and I am very surprised that it is about 1.3kb with a single item. Am I doing something wrong, or is there something else I should be doing? I have a large number of dictionaries and it is killing me to send them all across the wire. The code I use for serialization is
Dim dictionary As New Dictionary(Of Integer, Integer)
Dim stream As New MemoryStream
Dim bformatter As New BinaryFormatter()
dictionary.Add(1, 1)
bformatter.Serialize(stream, dictionary)
Dim len As Long = stream.Length
So, dictionary is faster because you used a better algorithm. Show activity on this post. The reason is because a dictionary is a lookup, while a list is an iteration. Dictionary uses a hash lookup, while your list requires walking through the list until it finds the result from beginning to the result each time.
Of course the Dictionary in principle has a faster lookup with O(1) while the lookup performance of a List is an O(n) operation. The Dictionary map a key to a value and cannot have duplicate keys, whereas a list just contains a collection of values. Also Lists allow duplicate items and support linear traversal.
The reason is dictionaries are very fast, implemented using a technique called hashing, which allows us to access a value very quickly. By contrast, the list of tuples implementation is slow. If we wanted to find a value associated with a key, we would have to iterate over every tuple, checking the 0th element.
A dictionary is 6.6 times faster than a list when we lookup in 100 items.
The default serialization for a dictionary has to include type information for the type of the dictionary, the comparer used, and for the types of each of the items (both key and value) because they might in general be subtypes. This overhead has to be added for each dictionary. If you print the data as a string you can see that there are a lot of fully qualified types taking up a lot of bytes:
\0\0\0\0????\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.Dictionary
2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0\aVersion\bComparer\bHashSize\rKeyValuePairs\0\0\b?System.Collections.Generic.GenericEqualityComparer
1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\b?System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]][]\0\0\0\t\0\0\0\0\0\0\t\0\0\0\0\0\0?System.Collections.Generic.GenericEqualityComparer
1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0\0\a\0\0\0\0\0\0\0\0\0\0?System.Collections.Generic.KeyValuePair2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]?????System.Collections.Generic.KeyValuePair
2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\0\0\0keyvalue\0\0\b\b\0\0\0\0\v
You might prefer to use a custom format for serialization, or else a standard format that is slightly lighter such as JSON.
There is a lot of overhead involved in setting up the dictionary for serialization (obviously, roughly 1.3kb ;) ). However, you'll find that the size doesn't grow dramatically as more elements are added to your collection, provided you're using primitive types for keys and values.
This overhead is mainly a one time, up front cost - so once you get the Dictionary class serialized, the contained members don't add as much size.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With