Skip to content

Instantly share code, notes, and snippets.

@brian-dot-net
Created March 13, 2014 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brian-dot-net/9519881 to your computer and use it in GitHub Desktop.
Save brian-dot-net/9519881 to your computer and use it in GitHub Desktop.
namespace ConsoleApplication1
{
using System;
using System.Diagnostics;
using System.Threading.Tasks;
internal sealed class Program
{
private static readonly Task CachedTask = Task.FromResult(false);
private static void Main(string[] args)
{
Bench(100000000);
}
private static void Bench(int iterations)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < iterations; i++)
{
PassThroughAsync().Wait();
}
double passThroughTime = stopwatch.Elapsed.TotalMilliseconds;
Console.WriteLine(passThroughTime);
stopwatch.Restart();
for (int i = 0; i < iterations; i++)
{
AwaitAsync().Wait();
}
double awaitTime = stopwatch.Elapsed.TotalMilliseconds;
Console.WriteLine(awaitTime);
Console.WriteLine("Pass through is {0:0.0} times faster than await.", awaitTime / passThroughTime);
}
private static Task PassThroughAsync()
{
return CachedTask;
}
private static async Task AwaitAsync()
{
await CachedTask;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment