Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Multithread, which performs the best? [closed]

I'm currently writting an application that make a huge lot of call to slow webservices (I had no say in that pattern) that produce little output. I'd like to make like 100 parallel calls (I know real parallelism can only go as far as you have cores). But I was wondering if they were performance differences between different approach.

I'm hesitating between:

  • Using Task.Factory.StartNew in a loop.
  • Using Parallel.For.
  • Using BackgroundWorker.
  • Using AsyncCallback.
  • ...Others?

My main goal, is to have as many webservices calls being started as quickly as possible. How should I proceed?

like image 605
Serge Avatar asked Feb 14 '26 06:02

Serge


2 Answers

From a performance standpoint it's unlikely to matter. As you yourself have described, the bottleneck in your program is a network call to a slow performing web service. That will be the bottleneck. Any differences in how long it takes you to spin up new threads or manage them is unlikely to matter at all due to how much they will be overshadowed by the network interaction.

You should use the model/framework that you are most comfortable with, and that will most effectively allow you to write code that you know is correct. It's also important to note that you don't actually need to use multiple threads on your machine at all. You can send a number of asynchronous requests to the web service all from the same thread, and even handle all of the callbacks in the same thread. Parallelizing the sending of requests is unlikely to have any meaningful performance impact. Because of this you don't really need to use any of the frameworks that you have described, although the Task Parallel Library is actually highly effective at managing asynchronous operations even when those operations don't represent work in another thread. You don't need it, but it's certainly capable of helping.

like image 182
Servy Avatar answered Feb 15 '26 19:02

Servy


According to your advices I used Async (with I/O event) while I was previously using TLP. Async really does outperform Sync + Task usage.

I can now launches 100 requests (almost?) at the same time and if the longest running one takes 5 seconds, the whole process will only last 7 seconds while when using Sync + TLP it took me like 70 seconds.

In conclusion, (auto generated) Async is really the way to go when consuming a lot of webservices.

Thanks to you all.

Oh and by the way, this would not be possible without:

<connectionManagement>
  <add address="*" maxconnection="100" />
</connectionManagement>
like image 44
Serge Avatar answered Feb 15 '26 19:02

Serge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!