I have a ASP MVC Controller contacting a bunch of http-sites in a for-loop. Like:
foreach(string provider in providers){
// get data from asomehttp URL
}
This takes about 4 seconds to perform...
I've tried:
Parallel.ForEach(providers, (provider) => {
// get data from some http URL
});
And i see NO performance gain at all!
Why is this?
The source collection is partitioned and the work is scheduled on multiple threads based on the system environment. The more processors on the system, the faster the parallel method runs. For some source collections, a sequential loop may be faster, depending on the size of the source, and the kind of work being performed. For more information about performance, see Potential Pitfalls in Data and Task Parallelism. Do Not Assume That Parallel Is Always Faster In certain cases a parallel loop might run slower than its sequential equivalent. The basic rule of thumb is that parallel loops that have few iterations and fast user delegates are unlikely to speedup much.
When parallelizing any code, including loops, one important goal is to utilize the processors as much as possible without over parallelizing to the point where the overhead for parallel processing negates any performance benefits. In this particular example ,only the outer loop is parallelized because there is not very much work performed in the inner loop. The combination of a small amount of work and undesirable cache effects can result in performance degradation in nested parallel loops. Therefore, parallelizing the outer loop only is the best way to maximize the benefits of concurrency on most systems.
To answer your question , it depends on the size of the source and the kind of work being performed.