Created
September 26, 2017 08:05
-
-
Save 20chan/2cb17d9afb364401e7a391dacf3e3602 to your computer and use it in GitHub Desktop.
Test async main in C#
This file contains hidden or 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
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