Question

I read that .NET C# has task and data parallelism built in. If I run a for/foreach loop in my program in a computer that has 4 cores (4 windows the Windows Task Manager), will the loop be evenly distributed into the 4 cores? If not, why isn't the for/foreach loop doesn't run in 4 cores parallel in default?

Generally speaking, is parallelism better than conventional programming? Does the benefits outweigh the drawbacks? I'm doing a lot of data processing (bunch of pulls, for/foreach loops) and need to complete these for/foreach loops faster.

Was it helpful?

Solution

it's not running on 4 cores because it is not efficent or needed every time.

it's depending on your program and the load of your program. it's not efficent to parallelize a function that did not take 1ms/cycle. if you do a lot of long running calcuations or searches over a big field of data that can be splitted up sensible you could / should parallelize your loops.

for that topic i recommend reading http://www.amazon.com/Multi-Core-Programming-Increasing-Performance-Multi-threading/dp/0976483246/ref=sr_1_fkmr0_2?ie=UTF8&qid=1336440123&sr=8-2-fkmr0

OTHER TIPS

If I run a for/foreach loop in my program in a computer that has 4 cores (4 windows the Windows Task Manager), will the loop be evenly distributed into the 4 cores?

Do you mean will the task be automatically parallelized over the cores? No. For that you need to use the Task Parallel Library such as Parallel.ForEach.

Generally speaking, is parallelism better than conventional programming?

It depends.

Does the benefits outweigh the drawbacks?

It depends.

I'm doing a lot of data processing (bunch of pulls, for/foreach loops) and need to complete these for/foreach loops faster.

Stop guessing that parellelization is your answer. Get out a profiler and find out where the bottleneck is. Parellelization won't help if, for example, the slowness stems from having to talk to a database over a network connection. It might help if the bottleneck are operations within the loop and those operations are parallelizable.

There is an implementation specifically for parallel processing (as @xbonez states).

.Net 4.0 does have an excellent parallel library (MSDN: TPL).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top