Skip to content

Instantly share code, notes, and snippets.

@benaadams
Created March 28, 2018 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benaadams/e7ec75119ec5eb98f56f17cfbaf2f0d4 to your computer and use it in GitHub Desktop.
Save benaadams/e7ec75119ec5eb98f56f17cfbaf2f0d4 to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Jobs;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Dict
{
[InProcess]
public class Program
{
public static void Main(string[] args)
{
valuesString = InitValuesArray();
dictionaryString = InitStringDict();
dictionaryInt = InitIntDict();
BenchmarkRunner.Run<Program>();
}
const int Size = 10000;
static string[] valuesString;
static Dictionary<string, string> dictionaryString;
static Dictionary<int, int> dictionaryInt;
private static string[] InitValuesArray()
{
var array = new string[Size];
for (int i = 0; i < array.Length; i++)
{
array[i] = i.ToString();
}
return array;
}
private static Dictionary<string, string> InitStringDict()
{
var dictionary = new Dictionary<string, string>();
for (int i = 0; i < Size; i++)
{
var s = i.ToString();
dictionary.Add(s, s);
}
return dictionary;
}
private static Dictionary<int, int> InitIntDict()
{
var dictionary = new Dictionary<int, int>();
for (int i = 0; i < Size; i++)
{
dictionary.Add(i, i);
}
return dictionary;
}
[Benchmark]
[MethodImpl(MethodImplOptions.NoInlining)]
public bool Dictionary_ContainsValue_String_True()
{
bool result = false;
var array = valuesString;
var dict = dictionaryString;
for (int i = 0; i < array.Length; i++)
{
result = dict.ContainsValue(array[i]); //Every value searched for is present in the dictionary.
}
return result;
}
[Benchmark]
[MethodImpl(MethodImplOptions.NoInlining)]
public bool Dictionary_ContainsValue_Int_True()
{
bool result = false;
var dict = dictionaryInt;
for (int i = 0; i < Size; i++)
{
result = dict.ContainsValue(i); //Every value searched for is present in the dictionary.
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment