Created
February 24, 2016 07:48
-
-
Save TIHan/3dfbab1b2f301051268e 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
class Benchmark | |
{ | |
[Test] | |
public void RunTypes() | |
{ | |
var cache = typeof(string).Assembly.GetTypes().ToDictionary(x => x, x => x.Name.Length); | |
var sum = 0; | |
var sw = Stopwatch.StartNew(); | |
var n = 1000000; | |
for (var i = 0; i < n; i++) | |
{ | |
switch (i % 3) | |
{ | |
case 0: | |
sum += cache[typeof(int)]; | |
continue; | |
case 1: | |
sum += cache[typeof(string)]; | |
continue; | |
case 2: | |
sum += cache[typeof(byte)]; | |
continue; | |
default: | |
throw new Exception(); | |
} | |
} | |
Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| sum += cache[typeof(...)] {n:N0} times took: {sw.ElapsedMilliseconds} ms"); | |
// 2016-02-24| sum += cache[typeof(...)] 1 000 000 times took: 29 ms | |
} | |
[Test] | |
public void RunInts() | |
{ | |
var count = typeof(string).Assembly.GetTypes().Length; | |
var cache = Enumerable.Range(0, count).ToDictionary(x => x, x => x); | |
var sum = 0; | |
var sw = Stopwatch.StartNew(); | |
var n = 1000000; | |
for (var i = 0; i < n; i++) | |
{ | |
sum += cache[i % 3]; | |
} | |
Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| sum += cache[i % 3] {n:N0} times took: {sw.ElapsedMilliseconds} ms"); | |
// 2016-02-24| sum += cache[i % 3] 1 000 000 times took: 17 ms | |
} | |
[Test] | |
public void RunConcurrentTypes() | |
{ | |
var cache = new ConcurrentDictionary<Type, int>(typeof(string).Assembly.GetTypes().ToDictionary(x => x, x => x.Name.Length)); | |
var sum = 0; | |
var sw = Stopwatch.StartNew(); | |
var n = 1000000; | |
for (var i = 0; i < n; i++) | |
{ | |
switch (i % 3) | |
{ | |
case 0: | |
sum += cache[typeof(int)]; | |
continue; | |
case 1: | |
sum += cache[typeof(string)]; | |
continue; | |
case 2: | |
sum += cache[typeof(byte)]; | |
continue; | |
default: | |
throw new Exception(); | |
} | |
} | |
Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| sum += cache[typeof(...)] {n:N0} times took: {sw.ElapsedMilliseconds} ms"); | |
// 2016-02-24| sum += cache[typeof(...)] 1 000 000 times took: 36 ms | |
} | |
[Test] | |
public void RunConcurrentInts() | |
{ | |
var count = typeof(string).Assembly.GetTypes().Length; | |
var cache = new ConcurrentDictionary<int, int>(Enumerable.Range(0, count).ToDictionary(x => x, x => x)); | |
var sum = 0; | |
var sw = Stopwatch.StartNew(); | |
var n = 1000000; | |
for (var i = 0; i < n; i++) | |
{ | |
sum += cache[i % 3]; | |
} | |
Console.WriteLine($"// {DateTime.Today.ToShortDateString()}| sum += cache[i % 3] {n:N0} times took: {sw.ElapsedMilliseconds} ms"); | |
// 2016-02-24| sum += cache[i % 3] 1 000 000 times took: 25 ms | |
} | |
} |
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
All run in release mode on an old laptop. Win7 .net 4.5. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment