最大程度地利用.NET任务并行库程度、任务、最大、NET

2023-09-03 01:42:58 作者:单手挽佳人

问题1

是使用的Parallel.For和Parallel.ForEach更适合与排序或任务工作无序?的

我之所以问的是,我最近更新的,其中一个StringBuilder被用来产生基于各种参数的SQL语句的串行循环。其结果是,SQL是有点混乱了(它包含的点语法错误)相比使用一个标准的foreach循环的时候,所以我的直觉是,TPL不适合执行任务,其中数据必须出现在特定的顺序。

My reason for asking is that I recently updated a serial loop where a StringBuilder was being used to generate a SQL statement based on various parameters. The result was that the SQL was a bit jumbled up (to the point it contained syntax errors) in comparison to when using a standard foreach loop, therefore my gut feeling is that TPL is not suited to performing tasks where the data must appear in a particular order.

问2。

是否TPL前自动执行?利用我必须提供什么多核架构的的

我之所以提出这个涉及回一个早期的教我问的问题与第三方物流业务的性能分析。这个问题的答案开导我的事实,TPL并不总是比一个标准串行环路更有效,因为该应用程序可能不能访问多个内核,并且因此造成额外线程和环路的开销会造成在比较的性能降低一个标准的串行循环。

My reason for asking this relates back to an eariler question I asked relating to performance profiling of TPL operations. An answer to the question enlightened me to the fact that TPL is not always more efficient than a standard serial loop as the application may not have access to multiple cores, and therefore the overhead of creating additional threads and loops would create a performance decrease in comparison to a standard serial loop.

推荐答案

我的直觉是,TPL不适合执行其中数据必须出现在一个特定的顺序任务。的

正确的。如果你希望的东西,才能,你可能有什么事情发生,当你并行的循环误解。

Correct. If you expect things in order, you might have a misunderstanding about what's going to happen when you "parallelize" a loop.

是否TPL前自动执行?利用我必须提供什么多核架构的的

请参阅MSDN杂志在下面的文章: http://msdn.microsoft.com/en-us/magazine/cc163340.aspx

See the following article on the msdn magazine: http://msdn.microsoft.com/en-us/magazine/cc163340.aspx

使用库,你可以方便地EX preSS潜在的并行   在现有的顺序code,其中暴露的并行任务会   同时在所有可用的处理器上运行。

Using the library, you can conveniently express potential parallelism in existing sequential code, where the exposed parallel tasks will be run concurrently on all available processors.