private static void CallVsCallVirt_Class(int count) { using (Benchmark.Run("Class", count)) { var cl = new ThisIsClass(); for (int i = 0; i < count; i++) { cl.Increment(); } } }
public void CallVsCallVirt(int r) { var count = 100000000; var sw = new Stopwatch(); sw.Restart(); int *ptr = stackalloc int[1]; int intValue = 0; for (int i = 0; i < count; i++) { *((int *)ptr) = *((int *)ptr) + 1; intValue = (*((int *)ptr)); } sw.Stop(); Console.WriteLine($"Value {sw.ElapsedMilliseconds} ({intValue})"); sw.Restart(); var str = new ThisIsSrtuct(new byte[4]); for (int i = 0; i < count; i++) { str.Increment(); } sw.Stop(); Console.WriteLine($"Struct {sw.ElapsedMilliseconds}"); sw.Restart(); IIncrementable strAsInterface = (IIncrementable)(new ThisIsSrtuct(new byte[4])); for (int i = 0; i < count; i++) { strAsInterface.Increment(); } sw.Stop(); Console.WriteLine($"Struct as Interface {sw.ElapsedMilliseconds}"); var constrainedStr = (new ThisIsSrtuct(new byte[4])); ConstrainedStruct(constrainedStr); sw.Restart(); for (int i = 0; i < count; i++) { ThisIsStaticClass.Increment(); } sw.Stop(); Console.WriteLine($"Static Class {sw.ElapsedMilliseconds}"); sw.Restart(); var cl = new ThisIsClass(); for (int i = 0; i < count; i++) { cl.Increment(); } sw.Stop(); Console.WriteLine($"Class {sw.ElapsedMilliseconds}"); sw.Restart(); var scl = new ThisIsSealedClass(); for (int i = 0; i < count; i++) { scl.Increment(); } sw.Stop(); Console.WriteLine($"Sealed Class {sw.ElapsedMilliseconds}"); sw.Restart(); IIncrementable cli = (IIncrementable) new ThisIsClass(); for (int i = 0; i < count; i++) { cli.Increment(); } sw.Stop(); Console.WriteLine($"Class as Interface {sw.ElapsedMilliseconds}"); sw.Restart(); var dcl = new ThisIsDerivedClass(); for (int i = 0; i < count; i++) { dcl.Increment(); } sw.Stop(); Console.WriteLine($"Derived Class {sw.ElapsedMilliseconds}"); 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}"); }
public ThisIsComposedClass() { value = new ThisIsClass(); }
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}"); }