using System; using System.Linq; using System.Collections.Generic; using System.Collections.Concurrent; using System.Reflection; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; for (int warmup = 0; warmup < 10; warmup++) _ = Work(100); int reference = Work(100); bool equivalent = true; for (int sample = 0; sample < 3; sample++) { long before = GC.GetAllocatedBytesForCurrentThread(); long start = System.Diagnostics.Stopwatch.GetTimestamp(); int result = Work(100); long ticks = System.Diagnostics.Stopwatch.GetTimestamp() - start; long bytes = GC.GetAllocatedBytesForCurrentThread() - before; equivalent &= result == reference && ticks >= 0 && bytes >= 0; Console.Error.WriteLine($"sample:{sample};ticks:{ticks};bytes:{bytes};checksum:{result}"); } Console.WriteLine($"equivalent:{equivalent}"); Console.WriteLine("samples:3"); static int Work(int count) { var values = new int[count]; for (int i = 0; i < values.Length; i++) values[i] = i; int total = 0; foreach (int value in values) total += value; GC.KeepAlive(values); return total; }