Comment

Comment by weswilson

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#.

parent weswilson 3 years ago

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton