using System.Diagnostics;
using System.Threading.Tasks;
public static async Task Main()
var (result1, elapsed1) = await TimeConsumingMethod1().GetElapsedTimeAsync();
Console.WriteLine($"Elapsed time1: {elapsed1}");
var (result2, elapsed2) = await TimeConsumingMethod2().GetElapsedTimeAsync();
Console.WriteLine($"Elapsed time1: {elapsed2}");
private static async Task<int> TimeConsumingMethod1()
private static async Task<int> TimeConsumingMethod2()
public static class SwExtensions
public static (T, long) GetElapsedTime<T>(Func<T> action)
var sw = new Stopwatch();
return (result, sw.ElapsedMilliseconds);
public static async Task<(T, long)> GetElapsedTimeAsync<T>(this Task<T> asyncAction)
var sw = new Stopwatch();
result = await asyncAction;
return (result, sw.ElapsedMilliseconds);