Stopwatch on Interlocked.Increment(ref x) vs. lock (y) { x++; }

Given 10 million iterations: object y = new object(); const int iterations = 10000000; Which is faster, InterlockedIncrement: stopWatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { Interlocked.Increment(ref x); } stopWatch.Stop(); Console.WriteLine("Interlocked took " + stopWatch.ElapsedMilliseconds); Or lock: stopWatch = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) {…

5