✅ 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?
14 Replies
It looks like you're entirely depending on the network there? Sure there's a bit of JSON deserialization, but network slowness is going to dominate
yes, ur right but maybe we can speed up json deserialization or replace string with span etc?
I mean, if you're spending 500ms doing 256 network requests (which doesn't seem to bad at all), 1 or 2ms on json stuff is going to make no observable difference
And that's quite conservative -- my guess is that the json stuff is taking sub-millisecond
hm, okay, by the way it was 2 seconds when I do this:
foreach(var chunk in chunks){ // send requests }
You were using an iterater and were dependent on the return from the PostAsync. As @canton7 said, network slowness and API responsiveness will be your issue.
So, foreach --> do 1 --> do http call --> get response --> do 2 --> etc.
Yeah, that's the difference between firing off 256 http requests in parallel, vs firing off one, then doing the second once the first had completed
With Task.WhenAll, you're allowing the ThreadPool scheduler to fire the threads when available; foreach, you're controlling when they fire and it's serially, so one after another.
hm can we say to ThreadPool to use 256 threads?
imagine we have these threads 😄
To give you an example of slowness, I call this API to get the temperature data. How slow the response is - I have no control over that. https://prodapi.metweb.ie/observations/Dublin/today
You're not blocking threads while the requests are in progress
That's the magic of async/await
To understand Task --> ThreadPool, see here: https://learn.microsoft.com/en-us/dotnet/standard/threading/the-managed-thread-pool#using-the-thread-pool
ah I forgot about that, thanks
another thing would be, that you could use something like
that way u dont have to first create a string with the whole response but parse the JSON directly.
this would reduce the copying of the whole response by one
thanks for helping me