|
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Utility;
using System.Diagnostics;
namespace Testing
{
[TestFixture]
public class TestDictionaryOnly
{
/// <summary>
/// Define test class to use key for
/// </summary>
public class TestClass
{
public string Column1 = null;
public string Column2 = null;
public TestClass(string Column1, string Column2)
{
this.Column1 = Column1;
this.Column2 = Column2;
}
}
//null keys are a problem that would have to be thought about on every usage
//dictionary objects need to be created for each additional key
public class PerfResults { public long initMS; public long lookupMS; }
public PerfResults GetTestPerf()
{
Dictionary<string, Dictionary<string, TestClass>> testLookups = new Dictionary<string, Dictionary<string, TestClass>>();
Stopwatch stopWatch = new Stopwatch();
PerfResults results = new PerfResults();
//test initialization
stopWatch.Start();
for (int i = 0; i < 1000000; i++)
{
TestClass testClass = new TestClass("Column1-" + i, "Column2-" + i);
Dictionary<string, TestClass> lookupItem = new Dictionary<string,TestClass>();
lookupItem.Add(testClass.Column2, testClass);
testLookups.Add(testClass.Column1, lookupItem);
}
stopWatch.Stop();
results.initMS = stopWatch.ElapsedMilliseconds;
//test getting
stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 1000000; i++)
{
TestClass testClass = new TestClass("Column1-" + i, "Column2-" + i);
//convenient syntax to just get, but if we want to do a containskey it is a pain
//unless we do below and catch exception - nasty
//TestClass lookupItem = testLookups[testClass.Column1][testClass.Column2];
//using containskey to keep similar to tests for other methods
//this get sfunky fast as more keys are used
if (testLookups.ContainsKey(testClass.Column1))
{
if (!testLookups[testClass.Column1].ContainsKey(testClass.Column2))
throw new ArgumentException("Can't find object we know is there");
}
else
{
throw new ArgumentException("Can't find object we know is there");
}
}
stopWatch.Stop();
results.lookupMS = stopWatch.ElapsedMilliseconds;
return results;
}
[Test]
public void TestPerf()
{
int testRuns = 10;
PerfResults totalResults = new PerfResults();
for (int i = 0; i < testRuns; i++)
{
PerfResults thisResult = GetTestPerf();
totalResults.initMS += thisResult.initMS;
totalResults.lookupMS += thisResult.lookupMS;
}
Debug.WriteLine("Initialization: " + (totalResults.initMS / testRuns).ToString("N0") + "ms");
Debug.WriteLine("Lookups: " + (totalResults.lookupMS / testRuns).ToString("N0") + "ms");
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.