Parallelism is a key concept in modern software development, enabling applications to perform multiple tasks simultaneously, thus improving performance and responsiveness. In C#, parallelism can be applied to both CPU-bound and I/O-bound operations, albeit using different techniques and tools. This blog will delve into these concepts, providing detailed explanations and code snippets.
CPU-bound Operations
CPU-bound operations are those that are limited by the processing power of the CPU. These tasks benefit from parallel execution, which can be achieved using tools like Parallel. For, Parallel.ForEach, and the Task Parallel Library (TPL).
πExplore more at: https://dotnet-fullstack-dev.blogspot.com/
π Sharing would be appreciated! π
Example Image Processing
Consider an example where we need to apply a filter to a large collection of images. This is a CPU-bound task because it requires significant processing power to manipulate the image data.
using System;
using System.Drawing;
using System.Threading.Tasks;
public class ImageProcessor
{
public static void ApplyFilter(string[] imagePaths)
{
Parallel.For(0, imagePaths.Length, i =>
{
using (Bitmap bitmap = new Bitmap(imagePaths[i]))
{
// Apply some filter to the image
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color originalColor = bitmap.GetPixel(x, y);
Color newColor = Color.FromArgb(originalColor.R / 2, originalColor.G / 2, originalColor.B / 2);
bitmap.SetPixel(x, y, newColor);
}
}
// Save the modified image
bitmap.Save($"filtered_{Path.GetFileName(imagePaths[i])}");
}
});
}
}
In this example, Parallel.For is used to apply the filter to each image in parallel. This allows multiple images to be processed simultaneously, leveraging multiple CPU cores and speeding up the overall processing time.
Difference Between Parallel.For and Parallel.ForEach.
- Parallel.For: This method is used for parallelizing a for loop, where the iteration index ranges from a start value to an end value. It is suitable when you need to control the loop index directly.
- Parallel.ForEach: This method is used for parallelizing operations over collections or enumerables. It is more flexible than Parallel. For as it can handle different types of collections.
Both methods execute iterations in parallel, but Parallel. Each is more adaptable for working with various data structures.
I/O-bound Operations
I/O-bound operations are those that are limited by the speed of external systems, such as disk I/O, network I/O, or database queries. For these tasks, asynchronous programming is more appropriate as it allows the application to continue executing other tasks while waiting for the I/O operation to complete.
Example Downloading Web Pages
Consider an example where we need to download the content of multiple web pages. This is an I/O-bound task because the performance is limited by the speed of the network and the web servers.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class WebPageDownloader
{
private static readonly HttpClient httpClient = new HttpClient();
public static async Task DownloadPagesAsync(string[] urls)
{
var downloadTasks = new List<Task<string>>();
foreach (var url in urls)
{
downloadTasks.Add(DownloadPageAsync(url));
}
var contents = await Task.WhenAll(downloadTasks);
for (int i = 0; i < urls.Length; i++)
{
string fileName = $"page_{i}.html";
await File.WriteAllTextAsync(fileName, contents[i]);
}
}
private static async Task<string> DownloadPageAsync(string url)
{
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
In this example, Task.WhenAll is used to download multiple web pages concurrently. Each download operation is performed asynchronously using the await keyword, allowing other tasks to run while waiting for the network response.
Why Use Task.WhenAll Instead of Parallel.ForEachAsync?
- Task.WhenAll is used to concurrently run multiple asynchronous tasks and wait for all of them to complete. This is particularly useful for I/O-bound operations where the tasks involve waiting for external resources.
- Parallel.ForEachAsync can be used for parallelizing asynchronous operations, but it is typically more suited for CPU-bound tasks or when you need to combine parallel and asynchronous execution in a more structured manner.
Combining CPU-bound and I/O-bound Operations
Sometimes, you might encounter scenarios where both CPU-bound and I/O-bound tasks need to be handled together. In such cases, you can combine parallel and asynchronous programming techniques.
Example Processing Data from a Web API
Consider an example where we need to fetch data from a web API, process the data, and then save the results to a database.
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
public class DataProcessor
{
private static readonly HttpClient httpClient = new HttpClient();
private const string ConnectionString = "your_connection_string_here";
public static async Task ProcessDataAsync(string[] urls)
{
var downloadTasks = new List<Task<string>>();
foreach (var url in urls)
{
downloadTasks.Add(DownloadDataAsync(url));
}
var dataList = await Task.WhenAll(downloadTasks);
Parallel.ForEach(dataList, data =>
{
var processedData = ProcessData(data);
SaveToDatabase(processedData).Wait();
});
}
private static async Task<string> DownloadDataAsync(string url)
{
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
private static ProcessedData ProcessData(string data)
{
// Simulate data processing
var jsonData = JsonSerializer.Deserialize<RawData>(data);
return new ProcessedData
{
Id = jsonData.Id,
Value = jsonData.Value * 2
};
}
private static async Task SaveToDatabase(ProcessedData data)
{
using (var connection = new SqlConnection(ConnectionString))
{
await connection.ExecuteAsync("INSERT INTO ProcessedData (Id, Value) VALUES (@Id, @Value)", data);
}
}
}
public class RawData
{
public int Id { get; set; }
public int Value { get; set; }
}
public class ProcessedData
{
public int Id { get; set; }
public int Value { get; set; }
}
In this example, we first download data from multiple URLs asynchronously. Once the data is downloaded, we process it in parallel using Parallel.ForEach. Finally, we save the processed data to the database using an asynchronous method.
Conclusion
Parallelism in C# can significantly improve the performance of both CPU-bound and I/O-bound operations. For CPU-bound tasks, use parallel constructs like Parallel.For and Parallel.ForEach. For I/O-bound tasks, leverage asynchronous programming with async and await. Combining these techniques can handle complex scenarios involving both types of operations efficiently.
Top comments (0)