简体   繁体   English

C#中的并行性

[英]Parallelism in C#

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? 我读到.NET C#具有内置的任务和数据并行性。如果我在具有4个核心(4个Windows Windows Task Manager)的计算机中的程序中运行for / foreach循环,该循环将均匀地分布到4个内核中核心? If not, why isn't the for/foreach loop doesn't run in 4 cores parallel in default? 如果不是,为什么默认情况下for / foreach循环不以4个内核并行运行?

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. 我正在做很多数据处理(拉动,for / foreach循环),并且需要更快地完成这些for / foreach循环。

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? 如果我在具有4个内核(Windows任务管理器有4个窗口)的计算机上的程序中运行for / foreach循环,该循环是否会平均分配到4个内核中?

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. 否。为此,您需要使用任务并行库,例如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. 我正在做很多数据处理(拉动,for / foreach循环),并且需要更快地完成这些for / foreach循环。

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). 有一个专门用于并行处理的实现(如@xbonez所述)。

.Net 4.0 does have an excellent parallel library (MSDN: TPL ). .Net 4.0确实具有出色的并行库(MSDN: TPL )。

it's not running on 4 cores because it is not efficent or needed every time. 它不是在4个内核上运行,因为它每次都不高效或不需要。

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. 并行化不占用1ms /周期的函数效率不高。 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 对于该主题,我建议您阅读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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM