Skip to content

Instantly share code, notes, and snippets.

@NVentimiglia
Last active August 25, 2016 05:58
Show Gist options
  • Save NVentimiglia/a84a44e70bd22ad74a3a44b8925b0c06 to your computer and use it in GitHub Desktop.
Save NVentimiglia/a84a44e70bd22ad74a3a44b8925b0c06 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using System.Diagnostics;
using System.Reflection;
public class ReflectionTester : MonoBehaviour
{
public class TestObject
{
public string Value { get; set; }
public void Handle(){}
}
[ContextMenu("DoTests")]
public void DoTest()
{
//Times in Ticks
UnityEngine.Debug.Log("Normal : "+ WithOutReflection(100)); // 1145 <-- Due to app startup ? Ignore first set.
UnityEngine.Debug.Log("Reflected : " + WithReflection(100)); //4087
UnityEngine.Debug.Log("Cached : " + WithCachedReflection(100)); //1487
UnityEngine.Debug.Log("Cached2 : " + WithCachedReflection2(100)); //35
UnityEngine.Debug.Log(" - ");
UnityEngine.Debug.Log("Normal : " + WithOutReflection(100)); //24
UnityEngine.Debug.Log("Reflected : " + WithReflection(100)); //2638
UnityEngine.Debug.Log("Cached : " + WithCachedReflection(100)); //1520
UnityEngine.Debug.Log("Cached2 : " + WithCachedReflection2(100)); //27
UnityEngine.Debug.Log(" - ");
UnityEngine.Debug.Log("Normal : " + WithOutReflection(100)); //30
UnityEngine.Debug.Log("Reflected : " + WithReflection(100)); //3729
UnityEngine.Debug.Log("Cached : " + WithCachedReflection(100)); //1303
UnityEngine.Debug.Log("Cached2 : " + WithCachedReflection2(100)); //27 <-- IDK, should retest with higher count
}
public long WithOutReflection(int count)
{
var instance = new TestObject();
var watch = new Stopwatch();
watch.Start();
for (int i = 0;i < count;i++)
{
instance.Handle();
instance.Value = "Hello";
}
watch.Stop();
return watch.ElapsedTicks;
}
public long WithReflection(int count)
{
var instance = new TestObject();
var watch = new Stopwatch();
watch.Start();
for (int i = 0;i < count;i++)
{
var type = instance.GetType();
var action = type.GetMethod("Handle");
var prop = type.GetProperty("Value");
action.Invoke(instance, null);
prop.SetValue(instance, "Hello", null);
}
watch.Stop();
return watch.ElapsedTicks;
}
public long WithCachedReflection(int count)
{
var instance = new TestObject();
var type = instance.GetType();
var action = type.GetMethod("Handle");
var prop = type.GetProperty("Value");
var watch = new Stopwatch();
watch.Start();
for (int i = 0;i < count;i++)
{
action.Invoke(instance, null);
prop.SetValue(instance, "Hello", null);
}
watch.Stop();
return watch.ElapsedTicks;
}
public long WithCachedReflection2(int count)
{
// Note this method can be used with data binding as we have a instance we can cache into a proxy
// For Injection, this might be more difficult.
// Then again we should be be injecting every frame. Only startup and view hydrating.
var instance = new TestObject();
PropertyInfo pinfo = typeof(TestObject).GetProperty("Value");
Action<string> prop = Delegate.CreateDelegate(typeof(Action<string>), instance, pinfo.GetSetMethod()) as Action<string>;
Action action = Delegate.CreateDelegate(typeof(Action), instance, "Handle") as Action;
var watch = new Stopwatch();
watch.Start();
for (int i = 0;i < count;i++)
{
action();
prop("Hello");
}
watch.Stop();
return watch.ElapsedTicks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment