Repository navigation
Parallel.ForEach should not be used for IO bound tasks #22957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
|
surenderssm marked this conversation as resolved.
|
||
|
|
||
| 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 hidden or 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.