Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BenchmarkSubject
- {
- public int InstanceField;
- public static Transform StaticField;
- public string InstanceProperty { get; set; }
- public static Vector3 StaticProperty { get; set; }
- public void InstanceMethod() { }
- public static void StaticMethod(float x) { }
- }
- // here's some benchmarks between directly calling fields/properties gettesr/setters and invoking methods,
- // then doing the same thing with reflection, then with the weakly typed API version, then the strong one!
- public string Benchmarks()
- {
- Action<string, Action> measure = (msg, code) =>
- {
- sw.Start();
- for (int i = 0; i < loops; i++)
- code();
- sw.Stop();
- builder.Append(msg);
- builder.Append(": ");
- builder.Append(sw.ElapsedMilliseconds.ToString());
- builder.AppendLine("ms");
- sw.Reset();
- };
- builder.Length = 0;
- object weak = strong;
- var xform = transform;
- var vec = Vector3.zero;
- Profiler.BeginSample("Direct");
- measure("Direct set instance field", () => strong.InstanceField = 10);
- measure("Direct get instance field", () => { var x = strong.InstanceField; });
- measure("Direct set instance property", () => strong.InstanceProperty = "Str");
- measure("Direct get instance property", () => { var x = strong.InstanceProperty; });
- measure("Direct set static field", () => BenchmarkSubject.StaticField = xform);
- measure("Direct get static field", () => { var x = BenchmarkSubject.StaticField; });
- measure("Direct set static property", () => BenchmarkSubject.StaticProperty = vec);
- measure("Direct get static property", () => { var x = BenchmarkSubject.StaticProperty; });
- measure("Direct invoke instance method", () => strong.InstanceMethod());
- measure("Direct invoke static method", () => BenchmarkSubject.StaticMethod(3.14f));
- Profiler.EndSample();
- builder.AppendLine("-----");
- Profiler.BeginSample("Reflection");
- measure("Reflection instance set field", () => instField.SetValue(strong, 10));
- measure("Reflection instance get field", () => instField.GetValue(strong));
- measure("Reflection instance set property", () => instProperty.SetValue(strong, "Str", null));
- measure("Reflection instance get property", () => instProperty.GetValue(strong, null));
- measure("Reflection static set field", () => staticField.SetValue(null, xform));
- measure("Reflection static get field", () => staticField.GetValue(null));
- measure("Reflection static set property", () => staticProperty.SetValue(null, vec, null));
- measure("Reflection static get property", () => staticProperty.GetValue(null, null));
- measure("Reflection invoke instance method", () => instanceMethod.Invoke(strong, null));
- measure("Reflection invoke static method", () => staticMethod.Invoke(null, args));
- Profiler.EndSample();
- builder.AppendLine("-----");
- // weakly typed API - deals with 'object'
- // very useful if you don't know the target and member types in advance
- var weakInstanceFieldSetter = instField.DelegateForSet();
- var weakInstanceFieldGetter = instField.DelegateForGet();
- var weakInstancePropertySetter = instProperty.DelegateForSet();
- var weakInstancePropertyGetter = instProperty.DelegateForGet();
- var weakStaticFieldSetter = staticField.DelegateForSet();
- var weakStaticFieldGetter = staticField.DelegateForGet();
- var weakStaticPropertySetter = staticProperty.DelegateForSet();
- var weakStaticPropertyGetter = staticProperty.DelegateForGet();
- var weakInstanceMethodCaller = instanceMethod.DelegateForCall();
- var weakStaticMethodCaller = staticMethod.DelegateForCall();
- Profiler.BeginSample("Weak Cached");
- measure("weak cached delegate instance set field", () => weakInstanceFieldSetter(ref weak, 10));
- measure("weak cached delegate instance get field", () => weakInstanceFieldGetter(weak));
- measure("weak cached delegate instance set property", () => weakInstancePropertySetter(ref weak, "Str"));
- measure("weak cached delegate instance get property", () => weakInstancePropertyGetter(weak));
- measure("weak cached delegate static set field", () => weakStaticFieldSetter(ref weak, xform));
- measure("weak cached delegate static get field", () => weakStaticFieldGetter(null));
- measure("weak cached delegate static set property", () => weakStaticPropertySetter(ref weak, vec));
- measure("weak cached delegate static get property", () => weakStaticPropertyGetter(null));
- measure("weak cached delegate invoke instance method", () => weakInstanceMethodCaller(weak, null));
- measure("weak cached delegate invoke static method", () => weakStaticMethodCaller(null, args));
- Profiler.EndSample();
- builder.AppendLine("-----");
- // strongly typed versions. only useful if you know in adavnce the target and member types!
- // (we'll talk to why would you use strong types in a moment)
- var strongInstanceFieldSetter = instField.DelegateForSet<BenchmarkSubject, int>();
- var strongInstanceFieldGetter = instField.DelegateForGet<BenchmarkSubject, int>();
- var strongInstancePropertySetter = instProperty.DelegateForSet<BenchmarkSubject, string>();
- var strongInstancePropertyGetter = instProperty.DelegateForGet<BenchmarkSubject, string>();
- var strongStaticFieldSetter = staticField.DelegateForSet<BenchmarkSubject, Transform>(); // doesn't actually matter what target type we specify, cause there's no target
- var strongStaticFieldGetter = staticField.DelegateForGet<BenchmarkSubject, Transform>();
- var strongStaticPropertySetter = staticProperty.DelegateForSet<BenchmarkSubject, Vector3>();
- var strongStaticPropertyGetter = staticProperty.DelegateForGet<BenchmarkSubject, Vector3>();
- var strongInstanceMethodCaller = instanceMethod.DelegateForCall<BenchmarkSubject, object>(); // object here means just no parameters
- var strongStaticMethodCaller = staticMethod.DelegateForCall<object, object>(); // method is static, doesn't matter what type we choose for the target. so we just wrote 'object'
- Profiler.BeginSample("Strong Cached");
- measure("strong cached delegate instance set field", () => strongInstanceFieldSetter(ref strong, 10));
- measure("strong cached delegate instance get field", () => strongInstanceFieldGetter(strong));
- measure("strong cached delegate instance set property", () => strongInstancePropertySetter(ref strong, "Str"));
- measure("strong cached delegate instance get property", () => strongInstancePropertyGetter(strong));
- measure("strong cached delegate static set field", () => strongStaticFieldSetter(ref strong, xform));
- measure("strong cached delegate static get field", () => strongStaticFieldGetter(null));
- measure("strong cached delegate static set property", () => strongStaticPropertySetter(ref strong, vec));
- measure("strong cached delegate static get property", () => strongStaticPropertyGetter(null));
- measure("strong cached delegate invoke instance method", () => strongInstanceMethodCaller(strong, null));
- measure("strong cached delegate invoke static method", () => strongStaticMethodCaller(null, args));
- Profiler.EndSample();
- return builder.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement