Created
August 8, 2019 15:56
-
-
Save JeffreyZhao/98a6b6a5e27422fac2cbcb59e42115e1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Collections.Generic; | |
using System.IO; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using BenchmarkDotNet.Attributes; | |
namespace Dict | |
{ | |
public class DictionaryPerf | |
{ | |
private Dictionary<int, int> _original; | |
private Dictionary<int, int> _deserialized; | |
public DictionaryPerf() | |
{ | |
_original = new Dictionary<int, int> { | |
{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, | |
{5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9} | |
}; | |
var formatter = new BinaryFormatter(); | |
var stream = new MemoryStream(); | |
formatter.Serialize(stream, _original); | |
stream.Position = 0; | |
_deserialized = (Dictionary<int, int>)formatter.Deserialize(stream); | |
} | |
[Benchmark(Baseline = true, OperationsPerInvoke = 10)] | |
public void Original() | |
{ | |
_original.ContainsKey(0); | |
_original.ContainsKey(1); | |
_original.ContainsKey(2); | |
_original.ContainsKey(3); | |
_original.ContainsKey(4); | |
_original.ContainsKey(5); | |
_original.ContainsKey(6); | |
_original.ContainsKey(7); | |
_original.ContainsKey(8); | |
_original.ContainsKey(9); | |
} | |
[Benchmark(OperationsPerInvoke = 10)] | |
public void Deserialized() | |
{ | |
_deserialized.ContainsKey(0); | |
_deserialized.ContainsKey(1); | |
_deserialized.ContainsKey(2); | |
_deserialized.ContainsKey(3); | |
_deserialized.ContainsKey(4); | |
_deserialized.ContainsKey(5); | |
_deserialized.ContainsKey(6); | |
_deserialized.ContainsKey(7); | |
_deserialized.ContainsKey(8); | |
_deserialized.ContainsKey(9); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment