Skip to content

Instantly share code, notes, and snippets.

@stephentoub
Last active June 6, 2017 23:07

Revisions

  1. stephentoub renamed this gist Jun 6, 2017. 1 changed file with 0 additions and 0 deletions.
  2. stephentoub created this gist Jun 6, 2017.
    36 changes: 36 additions & 0 deletions PerfBlog06062017_BinaryFormatter_Deserialize
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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;
    }
    }