-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallel.ForEach should not be used for IO bound tasks (#22957)
* Parallel.ForEach with CPU intensive operations * Refactored as per the code review comments
- Loading branch information
1 parent
060a8dc
commit 67ff206
Showing
5 changed files
with
151 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 76 additions & 32 deletions
108
samples/snippets/csharp/VS_Snippets_Misc/tpl_parallel/cs/simpleforeach.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,84 @@ | ||
// <snippet03> | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Drawing; | ||
|
||
public class Example | ||
namespace ParallelExample | ||
{ | ||
public static void Main() | ||
class Program | ||
{ | ||
// A simple source for demonstration purposes. Modify this path as necessary. | ||
string[] files = Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg"); | ||
string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified"; | ||
Directory.CreateDirectory(newDir); | ||
|
||
// Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body) | ||
Parallel.ForEach(files, (currentFile) => | ||
{ | ||
// The more computational work you do here, the greater | ||
// the speedup compared to a sequential foreach loop. | ||
string filename = Path.GetFileName(currentFile); | ||
var bitmap = new Bitmap(currentFile); | ||
|
||
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); | ||
bitmap.Save(Path.Combine(newDir, filename)); | ||
|
||
// Peek behind the scenes to see how work is parallelized. | ||
// But be aware: Thread contention for the Console slows down parallel loops!!! | ||
|
||
Console.WriteLine($"Processing {filename} on thread {Thread.CurrentThread.ManagedThreadId}"); | ||
//close lambda expression and method invocation | ||
}); | ||
|
||
// Keep the console window open in debug mode. | ||
Console.WriteLine("Processing complete. Press any key to exit."); | ||
Console.ReadKey(); | ||
static void Main() | ||
{ | ||
// 2 million | ||
var limit = 2_000_000; | ||
var numbers = Enumerable.Range(0, limit).ToList(); | ||
|
||
var watch = Stopwatch.StartNew(); | ||
var primeNumbersFromForeach = GetPrimeList(numbers); | ||
watch.Stop(); | ||
|
||
var watchForParallel = Stopwatch.StartNew(); | ||
var primeNumbersFromParallelForeach = GetPrimeListWithParallel(numbers); | ||
watchForParallel.Stop(); | ||
|
||
Console.WriteLine($"Classical foreach loop | Total prime numbers : {primeNumbersFromForeach.Count} | Time Taken : {watch.ElapsedMilliseconds} ms."); | ||
Console.WriteLine($"Parallel.ForEach loop | Total prime numbers : {primeNumbersFromParallelForeach.Count} | Time Taken : {watchForParallel.ElapsedMilliseconds} ms."); | ||
|
||
Console.WriteLine("Press any key to exit."); | ||
Console.ReadLine(); | ||
} | ||
|
||
/// <summary> | ||
/// GetPrimeList returns Prime numbers by using sequential ForEach | ||
/// </summary> | ||
/// <param name="inputs"></param> | ||
/// <returns></returns> | ||
private static IList<int> GetPrimeList(IList<int> numbers) => numbers.Where(IsPrime).ToList(); | ||
|
||
/// <summary> | ||
/// GetPrimeListWithParallel returns Prime numbers by using Parallel.ForEach | ||
/// </summary> | ||
/// <param name="numbers"></param> | ||
/// <returns></returns> | ||
private static IList<int> GetPrimeListWithParallel(IList<int> numbers) | ||
{ | ||
var primeNumbers = new ConcurrentBag<int>(); | ||
|
||
Parallel.ForEach(numbers, number => | ||
{ | ||
if (IsPrime(number)) | ||
{ | ||
primeNumbers.Add(number); | ||
} | ||
}); | ||
|
||
return primeNumbers.ToList(); | ||
} | ||
|
||
/// <summary> | ||
/// IsPrime returns true if number is Prime, else false.(https://en.wikipedia.org/wiki/Prime_number) | ||
/// </summary> | ||
/// <param name="number"></param> | ||
/// <returns></returns> | ||
private static bool IsPrime(int number) | ||
{ | ||
if (number < 2) | ||
{ | ||
return false; | ||
} | ||
|
||
for (var divisor = 2; divisor <= Math.Sqrt(number); divisor++) | ||
{ | ||
if (number % divisor == 0) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
// </snippet03> | ||
// </snippet03> |
7 changes: 7 additions & 0 deletions
7
samples/snippets/csharp/VS_Snippets_Misc/tpl_parallel/cs/simpleforeach.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
95 changes: 59 additions & 36 deletions
95
samples/snippets/visualbasic/VS_Snippets_Misc/tpl_parallel/vb/simpleforeach.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,60 @@ | ||
'<snippet03> | ||
Imports System.IO | ||
Imports System.Threading | ||
Imports System.Threading.Tasks | ||
Imports System.Drawing | ||
|
||
Module ForEachDemo | ||
|
||
Sub Main() | ||
' A simple source for demonstration purposes. Modify this path as necessary. | ||
Dim files As String() = Directory.GetFiles("C:\Users\Public\Pictures\Sample Pictures", "*.jpg") | ||
Dim newDir As String = "C:\Users\Public\Pictures\Sample Pictures\Modified" | ||
Directory.CreateDirectory(newDir) | ||
|
||
Parallel.ForEach(files, Sub(currentFile) | ||
' The more computational work you do here, the greater | ||
' the speedup compared to a sequential foreach loop. | ||
Dim filename As String = Path.GetFileName(currentFile) | ||
Dim bitmap As New Bitmap(currentFile) | ||
|
||
bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone) | ||
bitmap.Save(Path.Combine(newDir, filename)) | ||
|
||
' Peek behind the scenes to see how work is parallelized. | ||
' But be aware: Thread contention for the Console slows down parallel loops!!! | ||
|
||
Console.WriteLine($"Processing {filename} on thread {Thread.CurrentThread.ManagedThreadId}") | ||
'close lambda expression and method invocation | ||
End Sub) | ||
|
||
|
||
' Keep the console window open in debug mode. | ||
Console.WriteLine("Processing complete. Press any key to exit.") | ||
Console.ReadKey() | ||
End Sub | ||
End Module | ||
'</snippet03> | ||
Imports System.Collections.Concurrent | ||
|
||
Namespace ParallelExample | ||
Class Program | ||
Shared Sub Main() | ||
' 2 million | ||
Dim limit = 2_000_000 | ||
Dim numbers = Enumerable.Range(0, limit).ToList() | ||
|
||
Dim watch = Stopwatch.StartNew() | ||
Dim primeNumbersFromForeach = GetPrimeList(numbers) | ||
watch.Stop() | ||
|
||
Dim watchForParallel = Stopwatch.StartNew() | ||
Dim primeNumbersFromParallelForeach = GetPrimeListWithParallel(numbers) | ||
watchForParallel.Stop() | ||
|
||
Console.WriteLine($"Classical foreach loop | Total prime numbers : {primeNumbersFromForeach.Count} | Time Taken : {watch.ElapsedMilliseconds} ms.") | ||
Console.WriteLine($"Parallel.ForEach loop | Total prime numbers : {primeNumbersFromParallelForeach.Count} | Time Taken : {watchForParallel.ElapsedMilliseconds} ms.") | ||
|
||
Console.WriteLine("Press any key to exit.") | ||
Console.ReadLine() | ||
End Sub | ||
|
||
' GetPrimeList returns Prime numbers by using sequential ForEach | ||
Private Shared Function GetPrimeList(numbers As IList(Of Integer)) As IList(Of Integer) | ||
Return numbers.Where(AddressOf IsPrime).ToList() | ||
End Function | ||
|
||
' GetPrimeListWithParallel returns Prime numbers by using Parallel.ForEach | ||
Private Shared Function GetPrimeListWithParallel(numbers As IList(Of Integer)) As IList(Of Integer) | ||
Dim primeNumbers = New ConcurrentBag(Of Integer)() | ||
Parallel.ForEach(numbers, Sub(number) | ||
|
||
If IsPrime(number) Then | ||
primeNumbers.Add(number) | ||
End If | ||
End Sub) | ||
Return primeNumbers.ToList() | ||
End Function | ||
|
||
' IsPrime returns true if number is Prime, else false.(https://en.wikipedia.org/wiki/Prime_number) | ||
Private Shared Function IsPrime(number As Integer) As Boolean | ||
If number < 2 Then | ||
Return False | ||
End If | ||
|
||
For divisor = 2 To Math.Sqrt(number) | ||
|
||
If number Mod divisor = 0 Then | ||
Return False | ||
End If | ||
Next | ||
|
||
Return True | ||
End Function | ||
End Class | ||
End Namespace | ||
'</snippet03> |
7 changes: 7 additions & 0 deletions
7
samples/snippets/visualbasic/VS_Snippets_Misc/tpl_parallel/vb/simpleforeach.vbproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |