Skip to content

Instantly share code, notes, and snippets.

@stephentoub
Last active June 6, 2017 23:07
Timing BinaryFormatter.Deserialize
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
class Test
{
static void Main()
{
var books = new List<Book>();
for (int i = 0; i < 1_000_000; i++)
{
string id = i.ToString();
books.Add(new Book { Name = id, Id = id });
}
var formatter = new BinaryFormatter();
var mem = new MemoryStream();
formatter.Serialize(mem, books);
mem.Position = 0;
var sw = Stopwatch.StartNew();
formatter.Deserialize(mem);
sw.Stop();
Console.WriteLine(sw.Elapsed.TotalSeconds);
}
[Serializable]
private class Book
{
public string Name;
public string Id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment