Created
July 25, 2014 16:08
-
-
Save btjake/0d8d768f0a29d73e8c6b to your computer and use it in GitHub Desktop.
Dictionary vs ConcurrentDictionary Performance
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
System.Collections.Generic.Dictionary<int, string> regularDictionary = new System.Collections.Generic.Dictionary<int, string>(); | |
System.Collections.Concurrent.ConcurrentDictionary<int, string> concurrentDictionary = new System.Collections.Concurrent.ConcurrentDictionary<int, string>(); | |
void Main() | |
{ | |
for(int i = 10000; i < 10000000; i *= 10) | |
{ | |
regularDictionary = new System.Collections.Generic.Dictionary<int, string>(); | |
concurrentDictionary = new System.Collections.Concurrent.ConcurrentDictionary<int, string>(); | |
Wrapper(TestRegularDictionaryWrite, i).TotalMilliseconds.Dump(String.Format("milliseconds for {0} regular write", i)); | |
Wrapper(TestConcurrentDictionaryWrite, i).TotalMilliseconds.Dump(String.Format("milliseconds for {0} concurrent write", i)); | |
Wrapper(TestRegularDictionaryRead, i).TotalMilliseconds.Dump(String.Format("milliseconds for {0} regular read", i)); | |
Wrapper(TestConcurrentDicitonaryRead, i).TotalMilliseconds.Dump(String.Format("milliseconds for {0} concurrent read", i)); | |
Wrapper(TestRegularDictionaryContains, i).TotalMilliseconds.Dump(String.Format("milliseconds for {0} regular contains", i)); | |
Wrapper(TestConcurrentDicitonaryContains, i).TotalMilliseconds.Dump(String.Format("milliseconds for {0} concurrent contains", i)); | |
"-------------------------------------------".Dump(); | |
} | |
} | |
TimeSpan Wrapper(Action<int> toDo, int numIterations) | |
{ | |
var startTime = DateTime.Now; | |
for(int i = 0; i < numIterations; i++) | |
{ | |
toDo.Invoke(i); | |
} | |
return DateTime.Now.Subtract(startTime); | |
} | |
void TestRegularDictionaryWrite(int i) | |
{ | |
regularDictionary.Add(i, i.ToString()); | |
} | |
void TestConcurrentDictionaryWrite(int i) | |
{ | |
concurrentDictionary.TryAdd(i, i.ToString()); | |
} | |
void TestRegularDictionaryRead(int i) | |
{ | |
var str = regularDictionary[i]; | |
} | |
void TestConcurrentDicitonaryRead(int i) | |
{ | |
var str = concurrentDictionary[i]; | |
} | |
void TestRegularDictionaryContains(int i) | |
{ | |
var contains = regularDictionary.ContainsKey(i); | |
} | |
void TestConcurrentDicitonaryContains(int i) | |
{ | |
var contains = concurrentDictionary.ContainsKey(i); | |
} | |
//output | |
// | |
//milliseconds for 10000 regular write | |
//1.5002 | |
// | |
//milliseconds for 10000 concurrent write | |
//2.5003 | |
// | |
//milliseconds for 10000 regular read | |
//0.5001 | |
// | |
//milliseconds for 10000 concurrent read | |
//0.5 | |
// | |
//milliseconds for 10000 regular contains | |
//0.5001 | |
// | |
//milliseconds for 10000 concurrent contains | |
//0.5001 | |
// | |
//------------------------------------------- | |
// | |
//milliseconds for 100000 regular write | |
//20.0025 | |
// | |
//milliseconds for 100000 concurrent write | |
//31.0039 | |
// | |
//milliseconds for 100000 regular read | |
//2.5003 | |
// | |
//milliseconds for 100000 concurrent read | |
//3.0004 | |
// | |
//milliseconds for 100000 regular contains | |
//2.0002 | |
// | |
//milliseconds for 100000 concurrent contains | |
//3.0004 | |
// | |
//------------------------------------------- | |
// | |
//milliseconds for 1000000 regular write | |
//295.0375 | |
// | |
//milliseconds for 1000000 concurrent write | |
//785.5998 | |
// | |
//milliseconds for 1000000 regular read | |
//24.5031 | |
// | |
//milliseconds for 1000000 concurrent read | |
//37.0047 | |
// | |
//milliseconds for 1000000 regular contains | |
//22.5028 | |
// | |
//milliseconds for 1000000 concurrent contains | |
//31.5041 | |
// | |
//------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any reason why you didn't use a Stopwatch?