This channel was never meant to be just about stolen memes and jokes. One of the goals was to share some random pieces of knowledge (#random_PoK π) that I come across. Since I mostly work with .NET, theyβll likely be .NET-related β but weβll see.
So, here's one of them β a niche but interesting Task.WhenEach method. Added in .NET 9, it allows iterating over tasks as they complete via the async enumerable it returns. Here's a basic example:
I haven't found many production use cases for this yet but one I can imagine is processing a subset of tasks until some condition is met β similar to this SO question, which otherwise would require more verbose code.
I also composed a simple benchmark which showed no significant runtime difference compared to
That said, benchmarks like this are synthetic, so always test your actual workload on real hardware. There's also the question of how applicable microbenchmarking is to parallelized code π.
#csharp #dotnet
So, here's one of them β a niche but interesting Task.WhenEach method. Added in .NET 9, it allows iterating over tasks as they complete via the async enumerable it returns. Here's a basic example:
List<Task<string>> tasks = [ .. ]; // some tasks
await foreach (var completed in Task.WhenEach(tasks))
{
Console.WriteLine(completed.Result);
}
I haven't found many production use cases for this yet but one I can imagine is processing a subset of tasks until some condition is met β similar to this SO question, which otherwise would require more verbose code.
I also composed a simple benchmark which showed no significant runtime difference compared to
Task.WhenAll. However, the higher completed work items count and lock contentions are worth noting:| Method | Mean | Error | StdDev | Completed Work Items | Lock Contentions | Allocated |
|------------- |---------:|---------:|---------:|---------------------:|-----------------:|----------:|
| TaskWhenAll | 15.58 ms | 0.086 ms | 0.081 ms | 11.0000 | - | 3.99 KB |
| TaskWhenEach | 15.53 ms | 0.068 ms | 0.061 ms | 16.6250 | 0.0156 | 3.82 KB |
That said, benchmarks like this are synthetic, so always test your actual workload on real hardware. There's also the question of how applicable microbenchmarking is to parallelized code π.
#csharp #dotnet
π6π₯°1
gurustron is online!
Small stream - SO and remembering Apache Ignite SIMD stuff
https://www.twitch.tv/gurustron
Small stream - SO and remembering Apache Ignite SIMD stuff
https://www.twitch.tv/gurustron
β€2