Created
March 25, 2012 11:40
-
-
Save feanz/2193072 to your computer and use it in GitHub Desktop.
A set of extension methods for doing benchmarks
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
static class BenchmarkExtension { | |
public static void Times(this int times, string description, Action action) { | |
Stopwatch watch = new Stopwatch(); | |
watch.Start(); | |
for (int i = 0; i < times; i++) { | |
action(); | |
} | |
watch.Stop(); | |
Console.WriteLine("{0} ... Total time: {1}ms ({2} iterations)", | |
description, | |
watch.ElapsedMilliseconds, | |
times); | |
} | |
} | |
var randomStrings = Enumerable.Range(0, 10000) | |
.Select(_ => Guid.NewGuid().ToString()) | |
.ToArray(); | |
50.Times("Add 10,000 random strings to a Dictionary", | |
() => { | |
var dict = new Dictionary<string, object>(); | |
foreach (var str in randomStrings) { | |
dict.Add(str, null); | |
} | |
}); | |
50.Times("Add 10,000 random strings to a SortedList", | |
() => { | |
var list = new SortedList<string, object>(); | |
foreach (var str in randomStrings) { | |
list.Add(str, null); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment