-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProcessInParallelExample.cs
More file actions
112 lines (98 loc) · 4.45 KB
/
Copy pathProcessInParallelExample.cs
File metadata and controls
112 lines (98 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using EnumerableAsyncProcessor.Extensions;
namespace EnumerableAsyncProcessor.Example;
public static class ProcessInParallelExample
{
public static async Task RunExample()
{
Console.WriteLine("ProcessInParallel Extension Examples");
Console.WriteLine("====================================\n");
// Example 1: Simple parallel processing with an identity transformation
Console.WriteLine("Example 1: Simple parallel processing");
var asyncEnumerable1 = GenerateAsyncEnumerable(5);
IEnumerable<int> results1 = await asyncEnumerable1.ProcessInParallel(item => Task.FromResult(item));
Console.WriteLine($"Results: {string.Join(", ", results1)}");
// Example 2: Parallel processing with transformation
Console.WriteLine("\nExample 2: Parallel processing with transformation");
var asyncEnumerable2 = GenerateAsyncEnumerable(5);
var results2 = await asyncEnumerable2.ProcessInParallel(
async item =>
{
await Task.Delay(100); // Simulate async work
return item * 2;
});
Console.WriteLine($"Transformed results: {string.Join(", ", results2)}");
// Example 3: Parallel processing with max concurrency
Console.WriteLine("\nExample 3: Parallel processing with max concurrency (3)");
var asyncEnumerable3 = GenerateAsyncEnumerable(10);
var results3 = await asyncEnumerable3.ProcessInParallel(
async item =>
{
Console.WriteLine($"Processing item {item}");
await Task.Delay(100);
return $"Item-{item}";
},
maxConcurrency: 3);
Console.WriteLine($"Results count: {results3.Count()}");
// Example 4: Performance comparison
Console.WriteLine("\nExample 4: Performance comparison");
var itemCount = 20;
var asyncEnumerable4 = GenerateAsyncEnumerable(itemCount);
var start = DateTime.Now;
var sequentialResults = new List<int>();
await foreach (var item in asyncEnumerable4)
{
await Task.Delay(50); // Simulate work
sequentialResults.Add(item * 2);
}
var sequentialTime = DateTime.Now - start;
var asyncEnumerable5 = GenerateAsyncEnumerable(itemCount);
start = DateTime.Now;
var parallelResults = await asyncEnumerable5.ProcessInParallel(
async item =>
{
await Task.Delay(50); // Simulate work
return item * 2;
});
var parallelTime = DateTime.Now - start;
Console.WriteLine($"Sequential processing time: {sequentialTime.TotalMilliseconds:F0}ms");
Console.WriteLine($"Parallel processing time: {parallelTime.TotalMilliseconds:F0}ms");
Console.WriteLine($"Speedup: {sequentialTime.TotalMilliseconds / parallelTime.TotalMilliseconds:F1}x");
// Example 5: Proper disposal with builder pattern
Console.WriteLine("\nExample 5: Proper disposal when using builder pattern");
var data = Enumerable.Range(1, 10).ToArray();
// Using await using for automatic disposal
await using var processor = data
.SelectAsync(async item =>
{
await Task.Delay(50);
return item * 3;
}, CancellationToken.None)
.ProcessInParallel(maxConcurrency: 3);
// Process results as they become available
var processedCount = 0;
await foreach (var result in processor.GetResultsAsyncEnumerable())
{
processedCount++;
Console.WriteLine($"Processed item {processedCount}: {result}");
}
Console.WriteLine($"All {processedCount} items processed with proper disposal");
// Processor is automatically disposed here due to 'await using'
}
private static async IAsyncEnumerable<int> GenerateAsyncEnumerable(
int count,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
for (int i = 1; i <= count; i++)
{
await Task.Yield();
cancellationToken.ThrowIfCancellationRequested();
yield return i;
}
}
}