Slow BSON deserialization/deserialization

May 4, 2010 at 5:21 PM

It look like BSON serialization/deserialization for complex objects are much slower than DataContractSerializer. In my test I tried "AdvetureWorks/EF 4.0/Self Tracking Entities" (products = context.Products.Include("ProductProductPhotoes").ToArray();) and here are the results:


                                 DataContractSerializer                 BSON


Serialization:              37 msec                                     80 msec


Deserialization:          70 mscec                                    300 msec


Am I missing something?


Serialization:
            using (var ms = new MemoryStream())
            {
                var serializer = new JsonSerializer();
                var writer = new BsonWriter(ms);
                serializer.Serialize(writer, products);               
                productsByte = ms.ToArray();
                Console.WriteLine(String.Format("Elapsed time for BSON: {0}", watch.Elapsed));
            }
deserialization:
                var serializer = new JsonSerializer();
                Product[] products1;
                using (var ms = new MemoryStream(productsByte))
                {
                    var reader = new BsonReader(ms, true, DateTimeKind.Unspecified);
                    watch.Restart();
                    products1 = serializer.Deserialize<Product[]>(reader);
                }

Coordinator
May 5, 2010 at 1:58 AM

I don't know why. I'd really need to check it out in a profiler to see what is taking time.