Skip to content

Instantly share code, notes, and snippets.

@joncloud
Last active December 9, 2016 15:32
Show Gist options
  • Select an option

  • Save joncloud/e57baec905788980866099a11cff8644 to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/e57baec905788980866099a11cff8644 to your computer and use it in GitHub Desktop.
Dictionary/SortedList Memory
BenchmarkDotNet=v0.10.0
OS=Microsoft Windows NT 6.2.9200.0
Processor=Intel(R) Xeon(R) CPU E5-2673 v3 2.40GHzIntel(R) Xeon(R) CPU E5-2673 v3 2.40GHz, ProcessorCount=16
Frequency=10000000 Hz, Resolution=100.0000 ns, Timer=UNKNOWN
Host Runtime=Clr 4.0.30319.42000, Arch=32-bit RELEASE
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1073.0
Job Runtime(s):
        Clr 4.0.30319.42000, Arch=32-bit RELEASE
       Method | Count |              Mean |         StdDev |            Median | Gen 0 | Bytes Allocated/Op |

----------------- |------ |------------------ |--------------- |------------------ |------ |------------------- | Dictionary | 10 | 625.4147 ns | 15.0691 ns | 622.5707 ns | 0.09 | 566.56 | SortedDictionary | 10 | 4,664.4016 ns | 24.4736 ns | 4,657.4857 ns | 0.09 | 646.55 | SortedList | 10 | 5,816.9105 ns | 65.1338 ns | 5,819.5368 ns | 0.02 | 260.17 | Dictionary | 100 | 5,596.4410 ns | 9.5552 ns | 5,595.4262 ns | 0.74 | 4,704.19 | SortedDictionary | 100 | 113,332.8551 ns | 2,317.6236 ns | 112,151.8701 ns | 0.84 | 5,808.77 | SortedList | 100 | 89,846.8717 ns | 1,427.4924 ns | 89,163.2552 ns | - | 1,843.76 | Dictionary | 500 | 25,575.8976 ns | 256.5150 ns | 25,519.2342 ns | 2.99 | 25,627.52 | SortedDictionary | 500 | 790,586.3462 ns | 2,608.9533 ns | 790,730.1563 ns | - | 23,529.96 | SortedList | 500 | 538,657.6009 ns | 2,028.6820 ns | 538,785.5469 ns | - | 8,181.69 | Dictionary | 1000 | 53,693.2292 ns | 363.4642 ns | 53,526.9661 ns | 6.75 | 52,978.04 | SortedDictionary | 1000 | 1,779,827.2731 ns | 11,476.5862 ns | 1,776,770.9115 ns | - | 58,170.90 | SortedList | 1000 | 1,181,456.4063 ns | 3,474.5318 ns | 1,181,286.9011 ns | - | 20,102.19 | Dictionary | 2000 | 108,671.8813 ns | 434.3507 ns | 108,505.7454 ns | 12.47 | 101,459.32 | SortedDictionary | 2000 | 3,973,313.3110 ns | 21,757.8147 ns | 3,966,783.1770 ns | - | 91,666.82 | SortedList | 2000 | 2,576,671.1979 ns | 8,844.5485 ns | 2,572,104.2709 ns | - | 39,683.25 |

public class Dictionaries
{
[Params(10, 100, 500, 1000, 2000)]
public int Count;
List<Tuple<int, string, string, int, int>> tuples;
Dictionary<string, string> baseDictionary;
[Setup]
public void Setup()
{
tuples = CreateTuples();
baseDictionary = tuples.ToDictionary(tuple => tuple.Item2, tuple => tuple.Item2);
}
List<Tuple<int, string, string, int, int>> CreateTuples()
{
int count = Count;
var tuples = new List<Tuple<int, string, string, int, int>>(count);
while (--count >= 0)
{
string number = count.ToString("000000");
tuples.Add(Tuple.Create(count, number, new string(number.Reverse().ToArray()), count, count));
}
tuples.Sort((x, y) => x.Item2.CompareTo(y.Item2));
return tuples;
}
T Populate<T>(Func<Dictionary<string, string>, T> factory)
where T : IDictionary<string, string>
{
return factory(baseDictionary);
}
[Benchmark]
public Dictionary<string, string> Dictionary() => Populate(dict => new Dictionary<string, string>(dict));
[Benchmark]
public SortedDictionary<string, string> SortedDictionary() => Populate(dict => new SortedDictionary<string, string>(dict));
[Benchmark]
public SortedList<string, string> SortedList() => Populate(dict => new SortedList<string, string>(dict));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment