C
C#2y ago
SWEETPONY

✅ How to improve Parallel.ForEachAsync usage?

private async Task<ConcurrentDictionary<string, string>> GetIpAddresses( HashSet<string> deviceIds )
{
var devicesIpAddresses = new ConcurrentDictionary<string, string>();

await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
var archiveQuery = await _archiveClient
.Query( new QueryArguments
{
DeviceIDs = new HashSet<string> {deviceId},
Order = OrderType.Desc,
EventNames = new() {"nc100k:connected", "nc60k:connected", "nc8k:connected"},
Pagination = new() {PageSize = 1}
} )
.Unwrap();

var ip = archiveQuery.Items
.Select(verbalizedEvent => verbalizedEvent.Items
.FirstOrDefault(item => item.Type == "endpoint" )?.Title)
.FirstOrDefault() ?? "";

devicesIpAddresses[deviceId] = ip;
} );

return devicesIpAddresses;
}
private async Task<ConcurrentDictionary<string, string>> GetIpAddresses( HashSet<string> deviceIds )
{
var devicesIpAddresses = new ConcurrentDictionary<string, string>();

await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
var archiveQuery = await _archiveClient
.Query( new QueryArguments
{
DeviceIDs = new HashSet<string> {deviceId},
Order = OrderType.Desc,
EventNames = new() {"nc100k:connected", "nc60k:connected", "nc8k:connected"},
Pagination = new() {PageSize = 1}
} )
.Unwrap();

var ip = archiveQuery.Items
.Select(verbalizedEvent => verbalizedEvent.Items
.FirstOrDefault(item => item.Type == "endpoint" )?.Title)
.FirstOrDefault() ?? "";

devicesIpAddresses[deviceId] = ip;
} );

return devicesIpAddresses;
}
6 Replies
SWEETPONY
SWEETPONYOP2y ago
everything works for me, but I'm not sure if it's all well written
JakenVeina
JakenVeina2y ago
I question the value of using parallelism here at all I question the idea of returning a ConcurrentDictionary as opposed to an IDictionary or just Dictionary otherwise, seems fine
anita
anita2y ago
As with your last question, this does not look like CPU Bound work to me, so you shouldn’t be the Parallel class.
SWEETPONY
SWEETPONYOP2y ago
I tried simple foreach and parallel version second one was x2 faster 53 ms vs 106 Hmm
JakenVeina
JakenVeina2y ago
measured how? what did the foreach version look like?
SWEETPONY
SWEETPONYOP2y ago
1.
var timeWatch = new StopWatch();
timeWatch.Start();
foreach(var deviceIdin deviceIds)
{
await blah blah..
}
timeWatch.Stop();
2.
var timeWatch2 = new StopWatch();
timeWatch2.Start();
await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
await blah blah..
}
timeWatch2.Stop();
1.
var timeWatch = new StopWatch();
timeWatch.Start();
foreach(var deviceIdin deviceIds)
{
await blah blah..
}
timeWatch.Stop();
2.
var timeWatch2 = new StopWatch();
timeWatch2.Start();
await Parallel.ForEachAsync(deviceIds, async (deviceId, _) =>
{
await blah blah..
}
timeWatch2.Stop();
and the second one is faster ..
Want results from more Discord servers?
Add your server