Skip to content

Instantly share code, notes, and snippets.

@20chan
Created September 26, 2017 08:05
Show Gist options
  • Save 20chan/2cb17d9afb364401e7a391dacf3e3602 to your computer and use it in GitHub Desktop.
Save 20chan/2cb17d9afb364401e7a391dacf3e3602 to your computer and use it in GitHub Desktop.
Test async main in C#
class Program
{
static async Task Main(string[] args)
{
var t = new AsyncTest();
await t.DoTest();
}
}
class AsyncTest
{
public async Task DoTest()
{
Console.WriteLine($"Sync \t{Sync()} ms");
Console.WriteLine($"Async \t{await Async()} ms");
}
double Sync()
{
DateTime now = DateTime.Now;
int total = 0;
for (int i = 0; i < 100000; i++)
{
total += 1;
}
return (DateTime.Now - now).TotalMilliseconds;
}
async Task<double> Async()
{
DateTime now = DateTime.Now;
int total = 0;
await Task.Run(() =>
{
for (int i = 0; i < 100000; i++)
{
total += 1;
}
});
return (DateTime.Now - now).TotalMilliseconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment