© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago•
18 replies
SWEETPONY

✅ How to optimize this code?

Hello, I'd like to optimize my code. The best time I got: 00:00:00.536 with chunks count = 256
536ms actually is not too bad but is it possible to speed up method?
private async Task<IEnumerable<JsonNode?>?> GetDmnTasksAsync(IReadOnlyList<PassengerArrivalChunk> chunks)
{
    var tasks = chunks.Select(async chunk =>
    {
       var serializedChunk = Serializer.Serialize(new { passengerArrivalChunk = chunk });

       using var response = await httpClient
          .PostAsync(processingSettings.DmnUrl, new StringContent(serializedChunk, Encoding.UTF32, MediaTypeNames.Application.Json));

       var responseMessage = await response.Content.ReadAsStringAsync();
       var jsonArray = JsonNode.Parse(responseMessage)?["Tasks"]?.AsArray();

       return jsonArray?.Select(el => el).Where(x => x != null);
    }).ToList();

    var results = await Task.WhenAll(tasks);
    return results.SelectMany(result => result ?? []);
}
private async Task<IEnumerable<JsonNode?>?> GetDmnTasksAsync(IReadOnlyList<PassengerArrivalChunk> chunks)
{
    var tasks = chunks.Select(async chunk =>
    {
       var serializedChunk = Serializer.Serialize(new { passengerArrivalChunk = chunk });

       using var response = await httpClient
          .PostAsync(processingSettings.DmnUrl, new StringContent(serializedChunk, Encoding.UTF32, MediaTypeNames.Application.Json));

       var responseMessage = await response.Content.ReadAsStringAsync();
       var jsonArray = JsonNode.Parse(responseMessage)?["Tasks"]?.AsArray();

       return jsonArray?.Select(el => el).Where(x => x != null);
    }).ToList();

    var results = await Task.WhenAll(tasks);
    return results.SelectMany(result => result ?? []);
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to optimize this?
C#CC# / help
3y ago
✅ How to optimize this method?
C#CC# / help
2y ago
❔ Optimize Compiler Code Generation
C#CC# / help
4y ago