-
-
Save CharlieDigital/51e7457a01c5ade7c172771bdaf82e1c to your computer and use it in GitHub Desktop.
Comparing Parallel.For vs Channels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generate a set of 100 records, each with a random wait interval. | |
using System.Collections.Immutable; | |
using System.Diagnostics; | |
using System.Threading.Channels; | |
var log = (object msg) => Console.WriteLine(msg); | |
var workload = Enumerable | |
.Range(0, 100) | |
.Select(i => (Index: i, Delay: Random.Shared.Next(10, 50))) | |
.ToImmutableArray(); | |
// Using System.Threading.Channels | |
await InstrumentedRun("Channel", async () => { | |
var channel = Channel.CreateUnbounded<int>(); | |
async Task Run(ChannelWriter<int> writer, int id, int delay) { | |
await Task.Delay(delay); | |
await writer.WriteAsync(id); | |
} | |
async Task Receive(ChannelReader<int> reader) { | |
while (await reader.WaitToReadAsync()) { | |
if (reader.TryRead(out var id)) { | |
// No work here. | |
//log($" Completed {id}"); | |
} | |
} | |
} | |
var receiveTask = Receive(channel.Reader); | |
var processingTasks = workload | |
.AsParallel() | |
.Select(e => Run(channel.Writer, e.Index, e.Delay)); | |
await Task | |
.WhenAll(processingTasks) | |
.ContinueWith(_ => channel.Writer.Complete()); | |
await receiveTask; | |
}); | |
// Using Parallel.For with concurrency of 4 | |
await InstrumentedRun("Parallel.For @ 4", () => { | |
Parallel.For(0, 100, new ParallelOptions { MaxDegreeOfParallelism = 4 }, (index) => { | |
Thread.Sleep(workload[index].Delay); | |
}); | |
return Task.CompletedTask; | |
}); | |
// Using Parallel.ForEachAsync with concurrency of 4 | |
await InstrumentedRun("Parallel.ForEachAsync @ 4", async () => | |
await Parallel.ForEachAsync(workload, new ParallelOptions { MaxDegreeOfParallelism = 4 }, async (item, cancel) => { | |
await Task.Delay(item.Delay, cancel); | |
}) | |
); | |
// Using Parallel.ForEachAsync with concurrency of 40 | |
await InstrumentedRun("Parallel.ForEachAsync @ 40", async () => | |
await Parallel.ForEachAsync(workload, new ParallelOptions { MaxDegreeOfParallelism = 40 }, async (item, cancel) => { | |
await Task.Delay(item.Delay, cancel); | |
}) | |
); | |
// Using Parallel.ForEachAsync with concurrency unset | |
await InstrumentedRun("Parallel.ForEachAsync (Default)", async () => | |
await Parallel.ForEachAsync(workload, async (item, cancel) => { | |
await Task.Delay(item.Delay, cancel); | |
}) | |
); | |
/*----------------------------------------------------------- | |
* Supporting functions | |
---------------------------------------------------------*/ | |
async Task InstrumentedRun(string name, Func<Task> test) { | |
var threadsAtStart = Process.GetCurrentProcess().Threads.Count; | |
var timer = new Stopwatch(); | |
timer.Start(); | |
await test(); | |
timer.Stop(); | |
Console.WriteLine($"[{name}] = {timer.ElapsedMilliseconds}ms"); | |
Console.WriteLine($" ⮑ {threadsAtStart} threads at start"); | |
Console.WriteLine($" ⮑ {Process.GetCurrentProcess().Threads.Count} threads at end"); | |
} | |
/* | |
YMMV since each run uses a random workload. | |
[Channel] = 68ms | |
⮑ 8 threads at start | |
⮑ 19 threads at end | |
[Parallel.For @ 4] = 799ms | |
⮑ 19 threads at start | |
⮑ 19 threads at end | |
[Parallel.ForEachAsync @ 4] = 754ms | |
⮑ 19 threads at start | |
⮑ 19 threads at end | |
[Parallel.ForEachAsync @ 40] = 100ms | |
⮑ 19 threads at start | |
⮑ 19 threads at end | |
[Parallel.ForEachAsync (Default)] = 384ms | |
⮑ 19 threads at start | |
⮑ 19 threads at end | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nickgrishaev I wouldn't expect it to since the thread allocation has the # of cores as a factor and I'm on a Mac M1.