.net 4.0 Parallel Extensions
I have a method that dynamically makes a number of calls for each user control (.ascx). These calls in turn do lot of computation ( 3 - 4 secs ) for each user control. I was able to add a Parallel.For for looping thru user control and have the timing down to 5 - 6开发者_StackOverflow社区 secs.
Parallel.For(each user control )
{
compute();
}
List<int> compute()
{
for(i=0; i<999999;i++)
{.....}
}
I was wondering if it would make much difference if I used nested Parallel.For loops, so something like this.
Parallel.For(each user control )
{
compute();
}
List<int> compute()
{
Parallel.For(i=0; i<999999; ()=>
{.....}
}
Would nesting the Parallel structure help ?
This will depend on the number of controls.
If you have many controls (more than 2x number of CPU cores), nesting the Parallel.For loops will likely cause this to run much slower, as you're adding overhead that can't be resolved since you've already maximized the concurrency of your system.
If you only have one or two controls, however, and you have 4 or 8 or more cores in your system, then nesting may be beneficial.
If in doubt, I would recommend trying it and profiling to see if it helps. Just make sure to profile on different sets of hardware (to match your deployment targets), as concurrent profiling is very hardware dependent.
精彩评论