Parallel.For() with Interlocked.CompareExchange(): poorer performance and slightly different results to serial version
问题 I experimented with calculating the mean of a list using Parallel.For() . I decided against it as it is about four times slower than a simple serial version. Yet I am intrigued by the fact that it does not yield exactly the same result as the serial one and I thought it would be instructive to learn why. My code is: public static double Mean(this IList<double> list) { double sum = 0.0; Parallel.For(0, list.Count, i => { double initialSum; double incrementedSum; SpinWait spinWait = new