Skip to content

Instantly share code, notes, and snippets.

@tannergooding
Last active April 2, 2021 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tannergooding/cec01e88825230f5267884c7383ee3dc to your computer and use it in GitHub Desktop.
Save tannergooding/cec01e88825230f5267884c7383ee3dc to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
const int limit = 100_000_000;
static void Main(string[] args)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
int sum = 0;
for (int i = 0; i < limit; i++)
{
sum++;
}
sw.Stop();
Console.WriteLine($"Sum: {sum}; Serial Elapsed: {sw.Elapsed.TotalMilliseconds}"); // Sum: 100000000; Serial Elapsed: 28.9205
sw.Restart();
sum = 0;
Parallel.For(0, limit, (i) =>
{
Interlocked.Increment(ref sum);
});
sw.Stop();
Console.WriteLine($"Sum: {sum}; Parallel Interlocked Elapsed: {sw.Elapsed.TotalMilliseconds}"); // Sum: 100000000; Parallel Interlocked Elapsed: 1336.2358
sw.Restart();
sum = 0;
Parallel.For(0, Environment.ProcessorCount, (i) =>
{
var localSum = 0;
var batchSize = Math.DivRem(limit, Environment.ProcessorCount, out int rem);
if (i == Environment.ProcessorCount)
{
batchSize += rem;
}
for (int n = 0; n < batchSize; n++)
{
localSum++;
}
Interlocked.Add(ref sum, localSum);
});
sw.Stop();
Console.WriteLine($"Sum: {sum}; Parallel Batched Interlocked Elapsed: {sw.Elapsed.TotalMilliseconds}"); // Sum: 100000000; Parallel Batched Interlocked Elapsed: 2.7273
var localSums = new int[Environment.ProcessorCount];
sw.Restart();
sum = 0;
Parallel.For(0, Environment.ProcessorCount, (i) =>
{
var localSum = 0;
var batchSize = Math.DivRem(limit, Environment.ProcessorCount, out int rem);
if (i == Environment.ProcessorCount)
{
batchSize += rem;
}
for (int n = 0; n < batchSize; n++)
{
localSum++;
}
localSums[i] = localSum;
});
for (int i = 0; i < localSums.Length; i++)
{
sum += localSums[i];
}
sw.Stop();
Console.WriteLine($"Sum: {sum}; Parallel Batched Lock-Free Elapsed: {sw.Elapsed.TotalMilliseconds}"); // Sum: 100000000; Parallel Batched Lock-Free Elapsed: 2.365
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment