private static void CallVsCallVirt_Class_CAS(int count) { using (Benchmark.Run("Class+CASLock", count)) { var cl_ml = new ThisIsClassWithManualLock(); for (int i = 0; i < count; i++) { cl_ml.Increment(); } } }
public void CallVsCallVirt(int r) { var count = 100_000_000; // var sw = new Stopwatch(); using (Benchmark.Run("Value", count)) { int *ptr = stackalloc int[1]; int intValue = 0; for (int i = 0; i < count; i++) { *((int *)ptr) = *((int *)ptr) + 1; intValue = (*((int *)ptr)); } } using (Benchmark.Run("Struct", count)) { var str = new ThisIsSrtuct(new byte[4]); for (int i = 0; i < count; i++) { str.Increment(); } } using (Benchmark.Run("Str>Interface", count)) { IIncrementable strAsInterface = (IIncrementable)(new ThisIsSrtuct(new byte[4])); for (int i = 0; i < count; i++) { strAsInterface.Increment(); } } using (Benchmark.Run("Str>Constr", count)) { var constrainedStr = (new ThisIsSrtuct(new byte[4])); ConstrainedStruct(constrainedStr); } using (Benchmark.Run("Static Class", count)) { for (int i = 0; i < count; i++) { ThisIsStaticClass.Increment(); } } using (Benchmark.Run("Class", count)) { var cl = new ThisIsClass(); for (int i = 0; i < count; i++) { cl.Increment(); } } using (Benchmark.Run("Class+Lock", count)) { var cl_l = new ThisIsClassWithLock(); for (int i = 0; i < count; i++) { cl_l.Increment(); } } using (Benchmark.Run("Class+MLock", count)) { var cl_ml = new ThisIsClassWithManualLock(); for (int i = 0; i < count; i++) { cl_ml.Increment(); } } using (Benchmark.Run("Sealed Class", count)) { var scl = new ThisIsSealedClass(); for (int i = 0; i < count; i++) { scl.Increment(); } } using (Benchmark.Run("Class>IFace", count)) { IIncrementable cli = (IIncrementable) new ThisIsClass(); for (int i = 0; i < count; i++) { cli.Increment(); } } using (Benchmark.Run("Derived Class", count)) { var dcl = new ThisIsDerivedClass(); for (int i = 0; i < count; i++) { dcl.Increment(); } } //sw.Restart(); //IIncrementable dcli = (IIncrementable)new ThisIsDerivedClass(); //for (int i = 0; i < count; i++) //{ // dcli.Increment(); //} //sw.Stop(); //Console.WriteLine($"Derived Class as Interface {sw.ElapsedMilliseconds}"); //sw.Restart(); //var sdcl = new ThisIsSealedDerivedClass(); //for (int i = 0; i < count; i++) //{ // sdcl.Increment(); //} //sw.Stop(); //Console.WriteLine($"Sealed Derived Class {sw.ElapsedMilliseconds}"); //sw.Restart(); //IIncrementable sdcli = (IIncrementable)new ThisIsSealedDerivedClass(); //for (int i = 0; i < count; i++) //{ // sdcli.Increment(); //} //sw.Stop(); //Console.WriteLine($"Sealed Derived Class as Interface {sw.ElapsedMilliseconds}"); //sw.Restart(); //var comp = new ThisIsComposedClass(); //for (int i = 0; i < count; i++) //{ // comp.Increment(); //} //sw.Stop(); //Console.WriteLine($"Composed class {sw.ElapsedMilliseconds}"); }