Joan Venge
Joan Venge

Reputation: 331250

Best Serialization for a scenario where performance is paramount and data form is unimportant in .NET?

Which serialization should I use?

I need to store a large Dictionary with 100000+ elements, and I just need to save and load this data directly without caring whether it's binary or whether it's formatted or not.

Right now I am using the BinarySerializer but not sure if it's the most effective?

Please suggest better alternatives in the .NET standard libraries or an external library, preferably free.

EDIT: This is to serialize to disk and from it. The app is single threaded too.

Upvotes: 1

Views: 350

Answers (3)

jro
jro

Reputation: 7480

This is a bit of an open-ended question. Are you storing this in memory or writing it to disk? Does this execute in a multi-threaded (and perhaps multi-concurrent-access) environment? Context is important.

BinarySerializer is generally going to be pretty fast, and there are external libs that provide better compression such as ProtoBuffers. I've personally had good success with DataContractSerializer.

The great thing about all these options is that you can try all of them (relatively pain free) to learn for yourself what works in your environment and operation.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502016

Well, it will depend on what's in the dictionary - but if Protocol Buffers is flexible enough for you (you have to define your own types to serialize - it doesn't do all .NET types or anything like that), it's pretty darned fast.

For example, in protocol buffers I'd represent the dictionary as a message with a repeated key/value pair field. For ultimate speed you could use the CodedOutputStream and CodedInputStream to serialize/deserialize the dictionary directly rather than reading it all into memory separately first. Again, it'll depend on what the key/value types are though.

Upvotes: 4

Joel Martinez
Joel Martinez

Reputation: 47779

This is entirely a guess since I haven't profiled this (ie. which is what you should do to truly get your answer).

But my guess is that the binary serializer would give you the best performance. Both in size and speed.

Upvotes: 2

Related Questions