Created
March 13, 2014 00:51
-
-
Save brian-dot-net/9519881 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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