How and When to Use async and await in C#

Understanding When to Use async/await in C#

Introduction: Asynchronous programming has become an essential part of modern software development, allowing us to write efficient and responsive code. In C#, the async/await keywords provide a powerful way to work with asynchronous operations. However, it’s important to understand when to use async/await and when not to, as their effectiveness depends on the nature of the task at hand. In this article, we’ll explore different scenarios and examples to help you make informed decisions.

CPU-Bound Work

CPU-bound work refers to tasks that heavily utilize the CPU and have limited or no dependency on external resources. Examples include complex mathematical calculations, sorting algorithms, and encryption operations. Let’s consider the Fibonacci calculation as an example.

public class FibonacciCalculator
{
    public async Task<long> CalculateAsync(int n)
    {
        if (n <= 0)
        {
            throw new ArgumentException("Invalid input");
        }

        if (n == 1 || n == 2)
        {
            return 1;
        }

        long previous = 1;
        long current = 1;

        for (int i = 3; i <= n; i++)
        {
            long next = previous + current;
            previous = current;
            current = next;
        }

        return current;
    }

    public long Calculate(int n)
    {
        if (n <= 0)
        {
            throw new ArgumentException("Invalid input");
        }

        if (n == 1 || n == 2)
        {
            return 1;
        }

        long previous = 1;
        long current = 1;

        for (int i = 3; i <= n; i++)
        {
            long next = previous + current;
            previous = current;
            current = next;
        }

        return current;
    }
}

public class Program
{
    public static async Task Main()
    {
         int n = 4000000;

		var calculator = new FibonacciCalculator();

		// Measure execution time for the synchronous version
		var syncStopwatch = Stopwatch.StartNew();
		long syncResult = calculator.Calculate(n);
		syncStopwatch.Stop();

		Console.WriteLine($"Synchronous Execution Time: {syncStopwatch.ElapsedMilliseconds} ms");
		Console.WriteLine($"Fibonacci({n}) = {syncResult}");
		Console.WriteLine();

		// Measure execution time for the asynchronous version
		var asyncStopwatch = Stopwatch.StartNew();
		long asyncResult = await calculator.CalculateAsync(n);
		asyncStopwatch.Stop();

		Console.WriteLine($"Asynchronous Execution Time: {asyncStopwatch.ElapsedMilliseconds} ms");
		Console.WriteLine($"Fibonacci({n}) = {asyncResult}");
	}
}

In this example, we have a FibonacciCalculator class that calculates the Fibonacci sequence synchronously and asynchronously. We measure the execution time for both approaches and see that async await is not helping here

Synchronous Execution Time: 8 ms  
Fibonacci(4000000) = 6558868233897966651  
  
Asynchronous Execution Time: 14 ms  
Fibonacci(4000000) = 6558868233897966651

IO-Bound Work

IO-bound work involves tasks that primarily rely on external resources such as databases, web services, or file systems. These tasks often have high latency due to network or disk access. An example of IO-bound work is fetching data from multiple URLs using HttpClient.

public class DataFetcher
{
    public async Task<string> FetchDataAsync(string url)
    {
        using (var client = new HttpClient())
        {
            return await client.GetStringAsync(url);
        }
    }

    public string FetchData(string url)
    {
        using (var client = new HttpClient())
        {
            return client.GetStringAsync(url).Result;
        }
    }
}

public class Program
{
    private const int ExecutionCount = 10;

    public static async Task Main()
    {
        var urls = new string[]
        {
            "https://en.wikipedia.org/wiki/2023_Odisha_train_collision",
            "https://en.wikipedia.org/wiki/Gujarat_Titans",
            "https://en.wikipedia.org/wiki/Sandy_Koufax",
            // Add more URLs here
        };

        var fetcher = new DataFetcher();

        // Synchronous approach
        var syncStopwatch = Stopwatch.StartNew();
        for (int i = 0; i < ExecutionCount; i++)
        {
            foreach (var url in urls)
            {
                string data = fetcher.FetchData(url);
                Console.WriteLine($"Synchronous Data from {url}: {data.Length} characters");
            }
        }
        syncStopwatch.Stop();
		Console.WriteLine($"Synchronous Execution Time: {syncStopwatch.ElapsedMilliseconds / ExecutionCount} ms");
		Console.WriteLine();

		// Asynchronous approach
		var asyncStopwatch = Stopwatch.StartNew();
		for (int i = 0; i < ExecutionCount; i++)
		{
			foreach (var url in urls)
			{
				string data = await fetcher.FetchDataAsync(url);
				Console.WriteLine($"Asynchronous Data from {url}: {data.Length} characters");
			}
		}
		asyncStopwatch.Stop();
		Console.WriteLine($"Asynchronous Execution Time: {asyncStopwatch.ElapsedMilliseconds / ExecutionCount} ms");
	}
}

Here, we have a DataFetcher class that fetches data from URLs synchronously and asynchronously using HttpClient. We compare the execution time for both approaches.

Synchronous Execution Time: 2203 ms  

Asynchronous Execution Time: 2114 ms
Main  ThreadDataFetcher  InstanceHttpClientExecute Synchronous ApproachFetchData(url)GetStringAsync(url).ResultResponseResponseloop[Execution Count]Execute Asynchronous ApproachFetchDataAsync(url)GetStringAsync(url)TaskTaskPerform other operationsAwait TaskResponseloop[Execution Count]Main  ThreadDataFetcher  InstanceHttpClient

When to Use async/await

  1. When performing IO-bound operations: async/await allows the program to continue executing other tasks while waiting for IO operations to complete, improving overall responsiveness.

  2. When dealing with user interfaces: async/await helps keep the UI thread responsive, ensuring a smooth user experience by preventing blocking.

When Not to Use async/await

  1. CPU-bound operations: Since async/await primarily helps with IO-bound operations, it may not be advantageous for tasks that heavily utilize the CPU. In fact, using async/await in such cases can introduce unnecessary overhead.

  2. Fire-and-forget scenarios: If you don’t need to await the result of an asynchronous operation or handle any exceptions immediately, using async/await may not be necessary.

Conclusion

Async/await is a powerful tool for writing asynchronous code in C#. It significantly improves performance and responsiveness in IO-bound scenarios. However, it’s important to consider the nature of the task at hand when deciding whether to use async/await. CPU-bound work may not benefit significantly from async/await, and it’s crucial to measure and analyze performance to make informed decisions.

By understanding when to use async/await and when not to, you can write more efficient and responsive code while avoiding unnecessary complexity and overhead.

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru