Skip to content

Instantly share code, notes, and snippets.

@JeffreyZhao
Created November 11, 2009 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JeffreyZhao/231716 to your computer and use it in GitHub Desktop.
Save JeffreyZhao/231716 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Xml.Linq;
using System.Xml;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Reflection;
namespace CacheBenchmark
{
public static class Cache<T>
{
public static object Instance;
}
class Program
{
static void Main(string[] args)
{
InitGenericStorage();
InitNormalDictionary();
TestNormalDictionary(1);
CodeTimer.Initialize();
int iteration = 100 * 100 * 100 * 100;
CodeTimer.Time("Generic Storage", 1, () => TestGenericStorage(iteration));
CodeTimer.Time("Normal Dictionary", 1, () => TestNormalDictionary(iteration));
CodeTimer.Time("Simply Creation", 1, () => TestCreateObject(iteration));
Console.WriteLine("press enter to exit...");
Console.ReadLine();
}
private static void InitGenericStorage()
{
Cache<object>.Instance = null;
TestGenericStorage(1); // warm up;
}
private static void TestGenericStorage(int iteration)
{
for (int i = 0; i < iteration; i++)
{
var instance = Cache<object>.Instance;
}
}
private static Dictionary<Type, object> s_normalDict;
private static void InitNormalDictionary()
{
s_normalDict = new Dictionary<Type, object>();
s_normalDict[typeof(object)] = new object();
TestNormalDictionary(1); // warm up
}
private static void TestNormalDictionary(int iteration)
{
var key = typeof(object);
for (int i = 0; i < iteration; i++)
{
var instance = s_normalDict[key];
}
}
private static void TestCreateObject(int iteration)
{
for (int i = 0; i < iteration; i++)
{
var instance = new object();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment