Advertisement
vexe

DR Benchmarks

Mar 1st, 2015
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.61 KB | None | 0 0
  1.     public class BenchmarkSubject
  2.     {
  3.         public int InstanceField;
  4.         public static Transform StaticField;
  5.         public string InstanceProperty { get; set; }
  6.         public static Vector3 StaticProperty { get; set; }
  7.         public void InstanceMethod() { }
  8.         public static void StaticMethod(float x) { }
  9.     }
  10.  
  11.    // here's some benchmarks between directly calling fields/properties gettesr/setters and invoking methods,
  12.     // then doing the same thing with reflection, then with the weakly typed API version, then the strong one!
  13.     public string Benchmarks()
  14.     {
  15.         Action<string, Action> measure = (msg, code) =>
  16.         {
  17.             sw.Start();
  18.             for (int i = 0; i < loops; i++)
  19.                 code();
  20.             sw.Stop();
  21.  
  22.             builder.Append(msg);
  23.             builder.Append(": ");
  24.             builder.Append(sw.ElapsedMilliseconds.ToString());
  25.             builder.AppendLine("ms");
  26.  
  27.             sw.Reset();
  28.         };
  29.  
  30.         builder.Length = 0;
  31.  
  32.         object weak = strong;
  33.         var xform = transform;
  34.         var vec = Vector3.zero;
  35.  
  36.         Profiler.BeginSample("Direct");
  37.         measure("Direct set instance field", () => strong.InstanceField = 10);
  38.         measure("Direct get instance field", () => { var x = strong.InstanceField; });
  39.         measure("Direct set instance property", () => strong.InstanceProperty = "Str");
  40.         measure("Direct get instance property", () => { var x = strong.InstanceProperty; });
  41.         measure("Direct set static field", () => BenchmarkSubject.StaticField = xform);
  42.         measure("Direct get static field", () => { var x = BenchmarkSubject.StaticField; });
  43.         measure("Direct set static property", () => BenchmarkSubject.StaticProperty = vec);
  44.         measure("Direct get static property", () => { var x = BenchmarkSubject.StaticProperty; });
  45.         measure("Direct invoke instance method", () => strong.InstanceMethod());
  46.         measure("Direct invoke static method", () => BenchmarkSubject.StaticMethod(3.14f));
  47.         Profiler.EndSample();
  48.  
  49.         builder.AppendLine("-----");
  50.  
  51.         Profiler.BeginSample("Reflection");
  52.         measure("Reflection instance set field", () => instField.SetValue(strong, 10));
  53.         measure("Reflection instance get field", () => instField.GetValue(strong));
  54.         measure("Reflection instance set property", () => instProperty.SetValue(strong, "Str", null));
  55.         measure("Reflection instance get property", () => instProperty.GetValue(strong, null));
  56.         measure("Reflection static set field", () => staticField.SetValue(null, xform));
  57.         measure("Reflection static get field", () => staticField.GetValue(null));
  58.         measure("Reflection static set property", () => staticProperty.SetValue(null, vec, null));
  59.         measure("Reflection static get property", () => staticProperty.GetValue(null, null));
  60.         measure("Reflection invoke instance method", () => instanceMethod.Invoke(strong, null));
  61.         measure("Reflection invoke static method", () => staticMethod.Invoke(null, args));
  62.         Profiler.EndSample();
  63.  
  64.         builder.AppendLine("-----");
  65.  
  66.         // weakly typed API - deals with 'object'
  67.         // very useful if you don't know the target and member types in advance
  68.         var weakInstanceFieldSetter = instField.DelegateForSet();
  69.         var weakInstanceFieldGetter = instField.DelegateForGet();
  70.         var weakInstancePropertySetter = instProperty.DelegateForSet();
  71.         var weakInstancePropertyGetter = instProperty.DelegateForGet();
  72.         var weakStaticFieldSetter = staticField.DelegateForSet();
  73.         var weakStaticFieldGetter = staticField.DelegateForGet();
  74.         var weakStaticPropertySetter = staticProperty.DelegateForSet();
  75.         var weakStaticPropertyGetter = staticProperty.DelegateForGet();
  76.         var weakInstanceMethodCaller = instanceMethod.DelegateForCall();
  77.         var weakStaticMethodCaller = staticMethod.DelegateForCall();
  78.  
  79.         Profiler.BeginSample("Weak Cached");
  80.         measure("weak cached delegate instance set field", () => weakInstanceFieldSetter(ref weak, 10));
  81.         measure("weak cached delegate instance get field", () => weakInstanceFieldGetter(weak));
  82.         measure("weak cached delegate instance set property", () => weakInstancePropertySetter(ref weak, "Str"));
  83.         measure("weak cached delegate instance get property", () => weakInstancePropertyGetter(weak));
  84.         measure("weak cached delegate static set field", () => weakStaticFieldSetter(ref weak, xform));
  85.         measure("weak cached delegate static get field", () => weakStaticFieldGetter(null));
  86.         measure("weak cached delegate static set property", () => weakStaticPropertySetter(ref weak, vec));
  87.         measure("weak cached delegate static get property", () => weakStaticPropertyGetter(null));
  88.         measure("weak cached delegate invoke instance method", () => weakInstanceMethodCaller(weak, null));
  89.         measure("weak cached delegate invoke static method", () => weakStaticMethodCaller(null, args));
  90.         Profiler.EndSample();
  91.  
  92.         builder.AppendLine("-----");
  93.  
  94.         // strongly typed versions. only useful if you know in adavnce the target and member types!
  95.         // (we'll talk to why would you use strong types in a moment)
  96.         var strongInstanceFieldSetter = instField.DelegateForSet<BenchmarkSubject, int>();
  97.         var strongInstanceFieldGetter = instField.DelegateForGet<BenchmarkSubject, int>();
  98.         var strongInstancePropertySetter = instProperty.DelegateForSet<BenchmarkSubject, string>();
  99.         var strongInstancePropertyGetter = instProperty.DelegateForGet<BenchmarkSubject, string>();
  100.         var strongStaticFieldSetter = staticField.DelegateForSet<BenchmarkSubject, Transform>(); // doesn't actually matter what target type we specify, cause there's no target
  101.         var strongStaticFieldGetter = staticField.DelegateForGet<BenchmarkSubject, Transform>();
  102.         var strongStaticPropertySetter = staticProperty.DelegateForSet<BenchmarkSubject, Vector3>();
  103.         var strongStaticPropertyGetter = staticProperty.DelegateForGet<BenchmarkSubject, Vector3>();
  104.         var strongInstanceMethodCaller = instanceMethod.DelegateForCall<BenchmarkSubject, object>(); // object here means just no parameters
  105.         var strongStaticMethodCaller = staticMethod.DelegateForCall<object, object>(); // method is static, doesn't matter what type we choose for the target. so we just wrote 'object'
  106.  
  107.         Profiler.BeginSample("Strong Cached");
  108.         measure("strong cached delegate instance set field", () => strongInstanceFieldSetter(ref strong, 10));
  109.         measure("strong cached delegate instance get field", () => strongInstanceFieldGetter(strong));
  110.         measure("strong cached delegate instance set property", () => strongInstancePropertySetter(ref strong, "Str"));
  111.         measure("strong cached delegate instance get property", () => strongInstancePropertyGetter(strong));
  112.         measure("strong cached delegate static set field", () => strongStaticFieldSetter(ref strong, xform));
  113.         measure("strong cached delegate static get field", () => strongStaticFieldGetter(null));
  114.         measure("strong cached delegate static set property", () => strongStaticPropertySetter(ref strong, vec));
  115.         measure("strong cached delegate static get property", () => strongStaticPropertyGetter(null));
  116.         measure("strong cached delegate invoke instance method", () => strongInstanceMethodCaller(strong, null));
  117.         measure("strong cached delegate invoke static method", () => strongStaticMethodCaller(null, args));
  118.         Profiler.EndSample();
  119.  
  120.         return builder.ToString();
  121.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement