Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save khalidsalomao/7f3a6c3f6c514f584d6a to your computer and use it in GitHub Desktop.
Save khalidsalomao/7f3a6c3f6c514f584d6a to your computer and use it in GitHub Desktop.
LinqPad - C# Benchmark - Reflection GetProperties cache
<Query Kind="Program" />
void Main()
{
// initialization
if (System.Diagnostics.Debugger.IsAttached)
System.Diagnostics.Debugger.Break ();
Console.WriteLine ("=========================================");
Console.WriteLine ("Warm up phase ...");
// first call to discard JIT timming
Execute1 (1);
Execute2 (1);
// execute test
GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers ();
Console.WriteLine ("=========================================");
Execute1 (5000000);
GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers ();
Console.WriteLine ("=========================================");
Execute2 (5000000);
GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers ();
}
static void Execute1 (int count)
{
var testObject = new sample ();
long s = 0;
// start timer
var t = Stopwatch.StartNew ();
// code
for (int i = 0; i < count; i++)
{
var prop = GetTypeProperties (testObject.GetType ())[0];
s += prop.Name.Length;
}
// stop timer
t.Stop ();
Console.WriteLine ("Execute1 time = " + t.ElapsedMilliseconds + " ms");
Console.WriteLine(s);
}
static void Execute2 (int count)
{
var testObject = new sample ();
long s = 0;
// start timer
var t = Stopwatch.StartNew ();
// code
for (int i = 0; i < count; i++)
{
var prop = GetTypeProperties2 (testObject.GetType ())[0];
s += prop.Name.Length;
}
// stop timer
t.Stop ();
Console.WriteLine ("Execute2 time = " + t.ElapsedMilliseconds + " ms");
Console.WriteLine(s);
}
class baseClass
{
public string base1 { get;set;}
public string base2 { get;set;}
}
class sample:baseClass
{
public string id { get;set;}
public string field1 { get;set;}
public string field2 { get;set;}
public string field3 { get;set;}
public string field4 { get;set;}
public string field5 { get;set;}
}
static Dictionary<int, PropertyInfo[]> cache = new Dictionary<int, PropertyInfo[]> ();
/// <summary>
/// Gets the type properties.
/// </summary>
/// <param name="currentType">Type of the current.</param>
/// <param name="recursiveBaseType">Type of the recursive base.</param>
/// <param name="publicOnly">The public only.</param>
/// <param name="selectReadWrite">The select only properties with read and write capabilities.</param>
/// <returns></returns>
private static PropertyInfo[] GetTypeProperties (Type currentType)
{
return currentType.GetProperties (BindingFlags.Public | BindingFlags.Instance); // | BindingFlags.FlattenHierarchy
}
private static PropertyInfo[] GetTypeProperties2 (Type currentType)
{
PropertyInfo[] props;
if (cache.TryGetValue(currentType.GetHashCode(), out props))
return props;
props = currentType.GetProperties (BindingFlags.Public | BindingFlags.Instance); // | BindingFlags.FlattenHierarchy
cache.Add(currentType.GetHashCode(), props);
return props;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment