Created
December 11, 2017 03:57
-
-
Save benaadams/3e2ba5bbc0f265f7ac2dbd5a7f2b1b67 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 BenchmarkDotNet.Attributes; | |
| using BenchmarkDotNet.Attributes.Jobs; | |
| using BenchmarkDotNet.Running; | |
| namespace DictionaryPerf | |
| { | |
| [InProcess] | |
| public class Program | |
| { | |
| const int OperationsPerInvoke = 100; | |
| static Dictionary<int, int> _dictionarySimple; | |
| static Dictionary<int, int> _dictionaryCollisions; | |
| [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] | |
| public bool ContainsKeySimplePositive() | |
| { | |
| bool result = false; | |
| for(var i = 0; i < OperationsPerInvoke; i++) | |
| { | |
| result &= _dictionarySimple.ContainsKey(0); | |
| } | |
| return result; | |
| } | |
| [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] | |
| public bool ContainsKeySimpleNegative() | |
| { | |
| bool result = false; | |
| for (var i = 0; i < OperationsPerInvoke; i++) | |
| { | |
| result &= _dictionarySimple.ContainsKey(1); | |
| } | |
| return result; | |
| } | |
| [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] | |
| public bool ContainsKeyCollisionPositive() | |
| { | |
| bool result = false; | |
| for (var i = 0; i < OperationsPerInvoke; i++) | |
| { | |
| result &= _dictionaryCollisions.ContainsKey(0); | |
| } | |
| return result; | |
| } | |
| [Benchmark(OperationsPerInvoke = OperationsPerInvoke)] | |
| public bool ContainsKeyCollisionNegative() | |
| { | |
| bool result = false; | |
| for (var i = 0; i < OperationsPerInvoke; i++) | |
| { | |
| result &= _dictionaryCollisions.ContainsKey(49); | |
| } | |
| return result; | |
| } | |
| public static void Main(string[] args) | |
| { | |
| _dictionarySimple = InitalizeSimple(); | |
| _dictionaryCollisions = InitalizeCollisions(); | |
| BenchmarkRunner.Run<Program>(); | |
| } | |
| static Dictionary<int, int> InitalizeSimple() | |
| { | |
| var dict = new Dictionary<int, int>(); | |
| dict.Add(0, 0); | |
| return dict; | |
| } | |
| static Dictionary<int, int> InitalizeCollisions() | |
| { | |
| var dict = new Dictionary<int, int>(7); | |
| dict.Add(0, 0); | |
| dict.Add(7, 0); | |
| dict.Add(14, 0); | |
| dict.Add(21, 0); | |
| dict.Add(28, 0); | |
| dict.Add(35, 0); | |
| dict.Add(42, 0); | |
| return dict; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment