Problem
Parallel has me a little perplexed. ForEach. What is the meaning of parallel? What is the purpose of ForEach? Please do not include any Microsoft Developer Network (MSDN) links.
Here’s a simple illustration:
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 use Parallel.ForEach to rewrite this example?
Asked by SilverLight
Solution #1
Foreach loop:
Parallel.ForEach:
The difference between a regular foreach loop and a foreach loop with variables is seen in the following example.
Parallel.ForEach() Example
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
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
Answered by Jignesh.Raj
Solution #2
string[] lines = File.ReadAllLines(txtProxyListPath.Text);
List<string> list_lines = new List<string>(lines);
Parallel.ForEach(list_lines, line =>
{
//Your stuff
});
Answered by L.B
Solution #3
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
});
The lines will be parsed in parallel within the loop as a result of this. I produced a series on the TPL that contains a part on Parallel if you want a more detailed, less “reference centric” introduction to the Parallel class. ForEach.
Answered by Reed Copsey
Solution #4
Use the following code for large files (you are less memory hungry)
Parallel.ForEach(File.ReadLines(txtProxyListPath.Text), line => {
//Your stuff
});
Answered by Samuel LEMAITRE
Solution #5
These phrases were effective for me.
string[] lines = File.ReadAllLines(txtProxyListPath.Text);
var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 10 };
Parallel.ForEach(lines , options, (item) =>
{
//My Stuff
});
Answered by Prince Prasad
Post is based on https://stackoverflow.com/questions/12251874/how-can-i-convert-this-foreach-code-to-parallel-foreach