top | item 35369822

(no title)

weswilson | 2 years ago

If you refactor the GetSalesAndUseTaxForState function to an async function, you can optimize it further with this:

    ConcurrentDictionary<string, double> taxMap = new ConcurrentDictionary<string, double>();

    await Task.WhenAll(migrantList
        .SelectMany(m => m.StatesTraversed)
        .Distinct()
        .Select(async s => taxMap.TryAdd(s, await GetSalesAndUseTaxForStateAsync(s))));

    migrantList.ForEach(mti => Console.WriteLine($"Total Relocation Fee: {mti.StatesTraversed.Sum(s => taxMap[s]) + mti.TotalMilesTravelled * .15}"));
This gets pretty close to negligible overhead. The above ran in 0.519s, including the .5s await time of the "API" call.

I just wanted to show that there's an optimized solution that is also more concise in C#.

discuss

order

No comments yet.