Skip to content

Instantly share code, notes, and snippets.

@juanpaexpedite
Last active August 29, 2021 07:26
Show Gist options
  • Save juanpaexpedite/757e3b15dbdf5cb52453c7fe42effacf to your computer and use it in GitHub Desktop.
Save juanpaexpedite/757e3b15dbdf5cb52453c7fe42effacf to your computer and use it in GitHub Desktop.
Testing Dictionaries in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestDictionary
{
/*
Test for 100 iterations
Time elapsed for int : 00:00:00.0000121
Time elapsed for string: 00:00:00.0000152
Time elapsed for object: 00:00:00.0000116
Time elapsed for hash : 00:00:00.0000013
Test for 1000 iterations
Time elapsed for int : 00:00:00.0000082
Time elapsed for string: 00:00:00.0000275
Time elapsed for object: 00:00:00.0000280
Time elapsed for hash : 00:00:00.0000138
Test for 10000 iterations
Time elapsed for int : 00:00:00.0000816
Time elapsed for string: 00:00:00.0002489
Time elapsed for object: 00:00:00.0002582
Time elapsed for hash : 00:00:00.0001137
Test for 10000 iterations
Time elapsed for int : 00:00:00.0000815
Time elapsed for string: 00:00:00.0002512
Time elapsed for object: 00:00:00.0002564
Time elapsed for hash : 00:00:00.0001160
Test for 1000000 iterations
Time elapsed for int : 00:00:00.0112451
Time elapsed for string: 00:00:00.0661399
Time elapsed for object: 00:00:00.0572273
Time elapsed for hash : 00:00:00.0351004
*/
class Program
{
object obj = new object();
Dictionary<int, object> intDict = new Dictionary<int, object>();
Dictionary<string, object> stringDict = new Dictionary<string, object>();
Dictionary<object, object> objectDict = new Dictionary<object, object>();
Dictionary<int, object> hashDict = new Dictionary<int, object>();
int[] test = new int[] { 100, 1000, 10000, 10000, 1000000 };
int maxamount = 1000000;
List<string> results = new List<string>();
string prefix = "monkey";
public static void Main(string[] args)
{
Program instance = new Program();
instance.results = new List<string>();
instance.CacheKeys();
for(int i = 0; i < instance.test.Length ;i++)
{
instance.Test(instance.test[i]);
}
foreach(var line in instance.results)
{
Console.WriteLine(line);
}
Console.Read();
}
int[] ikeys;
string[] skeys;
int[] hkeys;
private void CacheKeys()
{
ikeys = new int[maxamount];
skeys = new string[maxamount];
hkeys = new int[maxamount];
for (int i = 0; i < maxamount; i++)
{
ikeys[i] = i;
skeys[i] = $"{prefix}{i}";
hkeys[i] = $"{prefix}{i}".GetHashCode();
}
}
private void Test(int iterations)
{
results.Add($"Test for {iterations} iterations");
intDict = new Dictionary<int, object>();
stringDict = new Dictionary<string, object>();
objectDict = new Dictionary<object, object>();
hashDict = new Dictionary<int, object>();
for (int i = 0; i < iterations; i++)
{
intDict.Add(ikeys[i], obj);
stringDict.Add(skeys[i], obj);
objectDict.Add(skeys[i], obj);
hashDict.Add(hkeys[i], obj);
}
IntTest(iterations);
StringTest(iterations);
ObjectTest(iterations);
HashTest(iterations);
}
private void IntTest(int iterations)
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
object test;
for (int i = 0; i < iterations; i++)
{
test = intDict[ikeys[i]];
}
stopwatch.Stop();
results.Add($"Time elapsed for int : {stopwatch.Elapsed}");
}
private void StringTest(int iterations)
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
object test;
for (int i = 0; i < iterations; i++)
{
test = stringDict[skeys[i]];
}
stopwatch.Stop();
results.Add($"Time elapsed for string: {stopwatch.Elapsed}");
}
private void ObjectTest(int iterations)
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
object test;
for (int i = 0; i < iterations; i++)
{
test = objectDict[skeys[i]];
}
stopwatch.Stop();
results.Add($"Time elapsed for object: {stopwatch.Elapsed}");
}
private void HashTest(int iterations)
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
object test;
for (int i = 0; i < iterations; i++)
{
test = hashDict[hkeys[i]];
}
stopwatch.Stop();
results.Add($"Time elapsed for hash : {stopwatch.Elapsed}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment