Last active
March 15, 2023 04:53
-
-
Save teoadal/59b34c73858cf5a8c8d895f488f295da to your computer and use it in GitHub Desktop.
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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Jobs; | |
namespace Storage.Benchmark.Glo; | |
[SimpleJob(RuntimeMoniker.Net60)] | |
[SimpleJob(RuntimeMoniker.NetCoreApp31)] | |
[SimpleJob(RuntimeMoniker.Net48)] | |
[MeanColumn, MemoryDiagnoser] | |
public class DictionaryAddBenchmark | |
{ | |
[Benchmark(Baseline = true)] | |
public int DictionaryAdd() | |
{ | |
var dictionary = new Dictionary<int, int>(16); | |
foreach (var number in _numbers) | |
{ | |
dictionary.Add(number, number); | |
} | |
return dictionary.Count; | |
} | |
[Benchmark] | |
public int GlossaryAdd() | |
{ | |
var glossary = new Glossary<int>(16); | |
foreach (var number in _numbers) | |
{ | |
glossary.Add(number, number); | |
} | |
return glossary.Length; | |
} | |
#region Configuration | |
private int[] _numbers = null!; | |
[GlobalSetup] | |
public void Init() | |
{ | |
const int count = 1021; | |
_numbers = Enumerable.Range(0, count).ToArray(); | |
Shuffle(_numbers, new Random(1234)); | |
} | |
private static void Shuffle<T>(T[] array, Random random) | |
{ | |
var n = array.Length; | |
while (n > 1) | |
{ | |
var k = random.Next(n--); | |
(array[n], array[k]) = (array[k], array[n]); | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment