C# 如何将此 foreach 代码转换为 Parallel.ForEach?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12251874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert this foreach code to Parallel.ForEach?
提问by SilverLight
I am a bit of confused about Parallel.ForEach.
What is Parallel.ForEachand what does it exactly do?
Please don't reference any MSDN link.
我有点困惑Parallel.ForEach。
什么是Parallel.ForEach,它究竟是做什么的?
请不要引用任何 MSDN 链接。
Here's a simple example :
这是一个简单的例子:
string[] lines = File.ReadAllLines(txtProxyListPath.Text);
List<string> list_lines = new List<string>(lines);
foreach (string line in list_lines)
{
//My Stuff
}
How can I rewrite this example with Parallel.ForEach?
我怎样才能用 重写这个例子Parallel.ForEach?
采纳答案by L.B
string[] lines = File.ReadAllLines(txtProxyListPath.Text);
List<string> list_lines = new List<string>(lines);
Parallel.ForEach(list_lines, line =>
{
//Your stuff
});
回答by Reed Copsey
string[] lines = File.ReadAllLines(txtProxyListPath.Text);
// No need for the list
// List<string> list_lines = new List<string>(lines);
Parallel.ForEach(lines, line =>
{
//My Stuff
});
This will cause the lines to be parsed in parallel, within the loop. If you want a more detailed, less "reference oriented" introduction to the Parallel class, I wrote a series on the TPL which includes a section on Parallel.ForEach.
这将导致在循环内并行解析行。如果您想要对 Parallel 类进行更详细、更少“面向参考”的介绍,我写了一个关于 TPL 的系列文章,其中包括一个关于 Parallel.ForEach的部分。
回答by Jignesh.Raj
Foreach loop:
Foreach循环:
- Iterations takes place sequentially, one by one
- foreach loop is run from a single Thread.
- foreach loop is defined in every framework of .NET
- Execution of slowprocesses can be slower, as they're run serially
- Process 2 can't start until 1 is done. Process 3 can't start until 2 & 1 are done...
- Execution of quickprocesses can be faster, as there is no threading overhead
- 迭代按顺序进行,一个接一个
- foreach 循环从单个线程运行。
- .NET 的每个框架中都定义了 foreach 循环
- 慢进程的执行可能会更慢,因为它们是串行运行的
- 进程 2 无法启动,直到 1 完成。过程 3 无法开始,直到 2 & 1 完成...
- 快速进程的执行速度会更快,因为没有线程开销
Parallel.ForEach:
Parallel.ForEach:
- Execution takes place in parallel way.
- Parallel.ForEach uses multiple Threads.
- Parallel.ForEach is defined in .Net 4.0 and above frameworks.
- Execution of slowprocesses can be faster, as they can be run in parallel
- Processes 1, 2, & 3 mayrun concurrently (see reused threads in example, below)
- Execution of quickprocesses can be slower, because of additional threading overhead
- 执行以并行方式进行。
- Parallel.ForEach 使用多个线程。
- Parallel.ForEach 在 .Net 4.0 及以上框架中定义。
- 慢进程的执行速度可以更快,因为它们可以并行运行
- 进程 1、2 和 3可以并发运行(参见下面示例中的重用线程)
- 由于额外的线程开销,快速进程的执行速度可能会更慢。
The following example clearly demonstrates the difference between traditional foreach loop and
下面的例子清楚地展示了传统 foreach 循环和
Parallel.ForEach() Example
Parallel.ForEach() 示例
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelForEachExample
{
class Program
{
static void Main()
{
string[] colors = {
"1. Red",
"2. Green",
"3. Blue",
"4. Yellow",
"5. White",
"6. Black",
"7. Violet",
"8. Brown",
"9. Orange",
"10. Pink"
};
Console.WriteLine("Traditional foreach loop\n");
//start the stopwatch for "for" loop
var sw = Stopwatch.StartNew();
foreach (string color in colors)
{
Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10);
}
Console.WriteLine("foreach loop execution time = {0} seconds\n", sw.Elapsed.TotalSeconds);
Console.WriteLine("Using Parallel.ForEach");
//start the stopwatch for "Parallel.ForEach"
sw = Stopwatch.StartNew();
Parallel.ForEach(colors, color =>
{
Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10);
}
);
Console.WriteLine("Parallel.ForEach() execution time = {0} seconds", sw.Elapsed.TotalSeconds);
Console.Read();
}
}
}
Output
输出
Traditional foreach loop
1. Red, Thread Id= 10
2. Green, Thread Id= 10
3. Blue, Thread Id= 10
4. Yellow, Thread Id= 10
5. White, Thread Id= 10
6. Black, Thread Id= 10
7. Violet, Thread Id= 10
8. Brown, Thread Id= 10
9. Orange, Thread Id= 10
10. Pink, Thread Id= 10
foreach loop execution time = 0.1054376 seconds
Using Parallel.ForEach example
使用 Parallel.ForEach 示例
1. Red, Thread Id= 10
3. Blue, Thread Id= 11
4. Yellow, Thread Id= 11
2. Green, Thread Id= 10
5. White, Thread Id= 12
7. Violet, Thread Id= 14
9. Orange, Thread Id= 13
6. Black, Thread Id= 11
8. Brown, Thread Id= 10
10. Pink, Thread Id= 12
Parallel.ForEach() execution time = 0.055976 seconds
回答by Samuel LEMAITRE
For big file use the following code (you are less memory hungry)
对于大文件,请使用以下代码(您不太需要内存)
Parallel.ForEach(File.ReadLines(txtProxyListPath.Text), line => {
//Your stuff
});
回答by Prince Prasad
These lines Worked for me.
这些线对我有用。
string[] lines = File.ReadAllLines(txtProxyListPath.Text);
var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 10 };
Parallel.ForEach(lines , options, (item) =>
{
//My Stuff
});

