Skip to content

Instantly share code, notes, and snippets.

@dresswithpockets
Created January 29, 2017 01:59
Show Gist options
  • Select an option

  • Save dresswithpockets/2a7faacaae50e4f2d80e0d84e2014de7 to your computer and use it in GitHub Desktop.

Select an option

Save dresswithpockets/2a7faacaae50e4f2d80e0d84e2014de7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics;
namespace Benchmarker {
public class Program {
static Dictionary<int, float> ContainsKey_Hits, ContainsKey_Misses;
static Dictionary<int, float> TryGetValue_Hits, TryGetValue_Misses;
static Dictionary<int, float> TryCatch_Hits, TryCatch_Misses;
static Dictionary<int, float> Return_Calls, Out_Calls;
static Dictionary<int, int> TestDictionary;
// we're going to assume that none of these will ever be less than 1
static int NumberOfRecursions, NumberOfPowers, ItemCount;
static void Main(string[] args) {
Console.WindowWidth = (int)(Console.LargestWindowWidth * 0.75);
NumberOfRecursions = 1000;
NumberOfPowers = 100000;
ContainsKey_Hits = new Dictionary<int, float>();
ContainsKey_Misses = new Dictionary<int, float>();
TryGetValue_Hits = new Dictionary<int, float>();
TryGetValue_Misses = new Dictionary<int, float>();
TryCatch_Hits = new Dictionary<int, float>();
TryCatch_Misses = new Dictionary<int, float>();
Return_Calls = new Dictionary<int, float>();
Out_Calls = new Dictionary<int, float>();
TestDictionary = new Dictionary<int, int>();
Console.WriteLine("| Items | CK Hit | CK Miss | TGV Hit | TGV Miss | TC Hit | TC Miss | Return | Out |");
for (int ic = 1; ic <= NumberOfPowers; ic += Convert.ToInt32(Math.Round(Math.Pow(5, ic.ToString().Length - 1), MidpointRounding.AwayFromZero))) {
float tickAvg = 0;
ItemCount = ic;
TestDictionary.Clear();
for (int i = 0; i < ItemCount; i++) {
TestDictionary.Add(i, i);
}
Console.Write("| {0}|", FillWithWhiteSpace(ItemCount.ToString(), 10));
tickAvg = BenchmarkFunction(ContainsKey_HitTest, NumberOfRecursions);
if (ContainsKey_Hits.ContainsKey(ItemCount)) ContainsKey_Hits[ItemCount] += tickAvg;
else ContainsKey_Hits.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
tickAvg = BenchmarkFunction(ContainsKey_MissTest, NumberOfRecursions);
if (ContainsKey_Misses.ContainsKey(ItemCount)) ContainsKey_Misses[ItemCount] += tickAvg;
else ContainsKey_Misses.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
tickAvg = BenchmarkFunction(TryGetValue_HitTest, NumberOfRecursions);
if (TryGetValue_Hits.ContainsKey(ItemCount)) TryGetValue_Hits[ItemCount] += tickAvg;
else TryGetValue_Hits.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
tickAvg = BenchmarkFunction(TryGetValue_MissTest, NumberOfRecursions);
if (TryGetValue_Misses.ContainsKey(ItemCount)) TryGetValue_Misses[ItemCount] += tickAvg;
else TryGetValue_Misses.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
tickAvg = BenchmarkFunction(TryCatch_HitTest, NumberOfRecursions);
if (TryCatch_Hits.ContainsKey(ItemCount)) TryCatch_Hits[ItemCount] += tickAvg;
else TryCatch_Hits.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
tickAvg = BenchmarkFunction(TryCatch_MissTest, NumberOfRecursions);
if (TryCatch_Misses.ContainsKey(ItemCount)) TryCatch_Misses[ItemCount] += tickAvg;
else TryCatch_Misses.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
tickAvg = BenchmarkFunction(Return_CallTest, NumberOfRecursions);
if (Return_Calls.ContainsKey(ItemCount)) Return_Calls[ItemCount] += tickAvg;
else Return_Calls.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
tickAvg = BenchmarkFunction(Out_CallTest, NumberOfRecursions);
if (Out_Calls.ContainsKey(ItemCount)) Out_Calls[ItemCount] += tickAvg;
else Out_Calls.Add(ItemCount, tickAvg);
Console.Write(" {0}|", FillWithWhiteSpace(((tickAvg / Stopwatch.Frequency) / ItemCount).ToString("0.####E-0"), 10));
Console.WriteLine("");
}
var filename = "data";
if (File.Exists(filename + ".csv")) {
var i = 0;
do {
i++;
}
while (File.Exists(filename + i.ToString() + ".csv"));
filename += i.ToString();
}
using (var sw = new StreamWriter(File.Create(filename + ".csv"))) {
for (int ic = 1; ic <= NumberOfPowers; ic += Convert.ToInt32(Math.Round(Math.Pow(5, ic.ToString().Length - 1), MidpointRounding.AwayFromZero))) {
var key = ic;
sw.Write("{0},", key.ToString());
sw.Write("{0},", ((ContainsKey_Hits[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
sw.Write("{0},", ((ContainsKey_Misses[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
sw.Write("{0},", ((TryGetValue_Hits[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
sw.Write("{0},", ((TryGetValue_Misses[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
sw.Write("{0},", ((TryCatch_Hits[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
sw.Write("{0},", ((TryCatch_Misses[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
sw.Write("{0},", ((Return_Calls[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
sw.WriteLine("{0}", ((Out_Calls[key] / Stopwatch.Frequency) / key).ToString("0.####E-0"));
}
}
}
static void ContainsKey_HitTest() {
for (int i = 0; i < ItemCount; i++) {
if (TestDictionary.ContainsKey(i)) {
var item = TestDictionary[i];
}
}
}
static void ContainsKey_MissTest() {
for (int i = 0; i < ItemCount; i++) {
if (TestDictionary.ContainsKey(0)) {
var item = TestDictionary[i];
}
}
}
static void TryGetValue_HitTest() {
for (int i = 0; i < ItemCount; i++) {
int item;
TestDictionary.TryGetValue(i, out item);
}
}
static void TryGetValue_MissTest() {
for (int i = 0; i < ItemCount; i++) {
int item;
TestDictionary.TryGetValue(0, out item);
}
}
static void TryCatch_HitTest() {
for (int i = 0; i < ItemCount; i++) {
try {
var item = TestDictionary[i];
}
catch (KeyNotFoundException knfe) { }
finally { }
}
}
static void TryCatch_MissTest() {
for (int i = 0; i < ItemCount; i++) {
try {
var item = TestDictionary[0];
}
catch (KeyNotFoundException knfe) { }
finally { }
}
}
static void Return_CallTest() {
var value = ReturnUseless();
}
static void Out_CallTest() {
int value;
OutUseless(out value);
}
static int ReturnUseless() {
ContainsKey_HitTest();
return 0;
}
static void OutUseless(out int value) {
ContainsKey_HitTest();
value = 0;
}
static float BenchmarkFunction(Action func, int recurse) {
var ticks = 0f;
Stopwatch sw;
for (int r = 0; r < recurse; r++) {
sw = Stopwatch.StartNew();
func?.Invoke();
sw.Stop();
ticks += sw.ElapsedTicks;
}
return ticks / recurse;
}
static string FillWithWhiteSpace(string str, int preferredLength) {
var filled = new StringBuilder();
filled.Append(str);
var wsCount = preferredLength - (str.Length);
for (int i = 0; i < wsCount; i++) filled.Append(" ");
return filled.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment