Created
October 21, 2019 19:06
-
-
Save jkotas/63e681b1433afdf75440d857701301ea to your computer and use it in GitHub Desktop.
Multi-threaded Task performance test
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
using System; | |
using System.Threading.Tasks; | |
class Program | |
{ | |
static async ValueTask<int> Step(int i) | |
{ | |
if (i >= 999) | |
await Task.Delay(1); | |
else | |
await Task.Yield(); | |
return i + 1; | |
} | |
static async Task Work() | |
{ | |
Random r = new Random(); | |
for (int i = 0; i < 100000; i++) | |
{ | |
await Step(r.Next(1000)); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
long total = 0; | |
for (int c = 1; ; c++) | |
{ | |
int start = Environment.TickCount; | |
Task[] tasks = new Task[Environment.ProcessorCount * 4]; | |
for (int i = 0; i < tasks.Length; i++) | |
{ | |
tasks[i] = Work(); | |
} | |
Task.WaitAll(tasks); | |
int end = Environment.TickCount; | |
total += (end - start); | |
Console.WriteLine($"{end - start} Average: {total / c}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment