Skip to content

Instantly share code, notes, and snippets.

@feanz
Created March 25, 2012 11:40
Show Gist options
  • Save feanz/2193072 to your computer and use it in GitHub Desktop.
Save feanz/2193072 to your computer and use it in GitHub Desktop.
A set of extension methods for doing benchmarks
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