Last active
June 6, 2017 23:07
Timing BinaryFormatter.Deserialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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