0

So one of my apps downloads webpages from the internet. Presently i am using multiple(5) threads to speed up the downloading of webpages

for (int i = 0; i < th; i++)
{
    Thread thread = new Thread(start);
    thread.Start()
}

and in the start function i am downloading webpages by HtttpWebRequest and WebResponse Combination. Is there any way i can speed up the process.

I am thinking of using Tasks instead of thread. Will it cause any meaningful increase in speed or even a decrease in the resource usage of my PC.

Would be really thankful on some guidance here.

Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • did you see that ? http://stackoverflow.com/questions/13429129/task-vs-thread-diffrences – nha Oct 12 '13 at 15:59
  • It's almost certain that the majority of your time is being spent in the download itself. The limiting factor is the rate at which data is transferred from those web servers to your computer. The performance difference between explicitly managed threads and tasks will be milliseconds, at most. You will see no appreciable difference between the two, assuming that you're using them correctly. – Jim Mischel Oct 14 '13 at 21:21

2 Answers2

0

From MSDN:

Behind the scenes, tasks are queued to the ThreadPool, which has been enhanced with algorithms that determine and adjust to the number of threads and that provide load balancing to maximize throughput. This makes tasks relatively lightweight, and you can create many of them to enable fine-grained parallelism.

In other words, it will most likely be more efficient to use Tasks, because it uses pooled threads rather than creating new ones, and can automatically determine how many threads to create depending on the system it is running on - 5 might not be the optimal number of threads on all systems.

It is worth noting that by default, only two concurrent HTTP requests are allowed. To increase this, you need to change the value of ServicePointManager.DefaultConnectionLimit before initiating the requests.

sga101
  • 1,904
  • 13
  • 12
  • 1
    The amount of time it takes to start up a new thread pales in comparison to the time needed to perform several network requests. The difference is likely to be entirely negligible. Also, the thread pool *shouldn't* be used for long running tasks, such as a long network request like this; they should be used for short lived tasks. Odds are the TP will need to spin up the new tasks to handle this. – Servy Oct 14 '13 at 21:01
  • living in less developed country my connection speed is just 1mbps. However i didn't knew there was a deafult connection limit of just two. I will increase the limit and check if that makes any difference. – Win Coder Oct 17 '13 at 07:37
0

Using Task over Thread directly may make your code easier for you two write, understand, and work with. It is not likely to have any meaningful performance differences, especially in a context like this.

By default, new tasks will use the thread pool, and there are some performance differences between using a thread pool versus regular tasks, but given that your tasks are few in number, and long running, those differences will not be a large factor in this case.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • One dumb question. For example if i want multiple concurrent HTTP requests i make multiple threads. For tasks should i make multiple tasks or is there something else i can do ? – Win Coder Oct 17 '13 at 07:42
  • @WinCoder You make multiple tasks. – Servy Oct 17 '13 at 13:31