Skip to content

Instantly share code, notes, and snippets.

@jkotas
Created October 21, 2019 19:06
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 jkotas/63e681b1433afdf75440d857701301ea to your computer and use it in GitHub Desktop.
Save jkotas/63e681b1433afdf75440d857701301ea to your computer and use it in GitHub Desktop.
Multi-threaded Task performance test
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