using System; using System.Diagnostics; namespace ConsoleApp1 { class Program { static void Main(string[] args) { // stopwatch.Restart(); //benchmark.Run(); // time = stopwatch.ElapsedTicks; //Benchmarks.MainFake(); //var watch = Stopwatch.StartNew(); //BenchGC.RunClasses(); //watch.Stop(); //Console.WriteLine(watch.ElapsedTicks); GC.Collect(); var watch = Stopwatch.StartNew(); BenchGC.RunStrings(); watch.Stop(); Console.WriteLine(watch.ElapsedTicks); GC.Collect(); watch = Stopwatch.StartNew(); BenchGC.RunClasses(); watch.Stop(); Console.WriteLine(watch.ElapsedTicks); GC.Collect(); watch = Stopwatch.StartNew(); BenchGC.RunArrays(); watch.Stop(); Console.WriteLine(watch.ElapsedTicks); Console.ReadLine(); } } class BenchGC { public static int RunStrings() { string text = "Hello"; int a = 0; for(int i = 0; i < 1_000_000_000; i++) { text = text.Substring(0, text.Length - 1); a += text.Length; text += "a"; } return text.Length; } public static float RunClasses() { ClassTest test; float a= 0; for(int j = 0; j < 5; j++) { for(int i = 0; i < 1_000_000_000; i++) { test = new ClassTest(); test.Test1 = i; a += test.Test1; } } return a; } public static float RunStructs() { StructTest test; float a= 0; for(int i = 0; i < 1_000_000_000; i++) { test = new StructTest(); test.Test1 = i; a += test.Test1; } return a; } public static int RunArrays() { int[] test; int a = 0; for(int i = 0; i < 300_000; i++) { test = new int[i]; a += test.Length; } return a; } } class ClassTest { public float Test1; public float Test2; public float Test3; public float Test4; public float Test5; public float Test6; public float Test7; public float Test8; public float Test9; public float Test10; } struct StructTest { public float Test1; public float Test2; public float Test3; public float Test4; public float Test5; public float Test6; public float Test7; public float Test8; public float Test9; public float Test10; } }