Skip to content

Instantly share code, notes, and snippets.

@sholev
Created March 7, 2016 17:50
Show Gist options
  • Save sholev/04517354952276be28df to your computer and use it in GitHub Desktop.
Save sholev/04517354952276be28df to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
class TimedWorker
{
private readonly Stopwatch timer;
public TimedWorker()
{
this.timer = new Stopwatch();
}
public int Work()
{
int workDone = 0;
if (!this.timer.IsRunning)
{
this.timer.Start();
}
if (this.timer.ElapsedMilliseconds > 5)
{
workDone++;
this.timer.Restart();
}
return workDone;
}
}
static class Tests
{
static void Main(string[] args)
{
var thirtyWorkersResult = TestWorkers(30) / 10.0;
var fifteenWorkersResult = TestWorkers(15) / 10.0;
var nineWorkersResult = TestWorkers(9) / 10.0;
Console.WriteLine("30 workers done in " + thirtyWorkersResult);
Console.WriteLine("15 workers done in " + fifteenWorkersResult);
Console.WriteLine("9 workers done in " + nineWorkersResult);
}
static double TestWorkers(int numberOfWorkers)
{
var totalWorkload = new[] { 3740 };
var timer = new Stopwatch();
var workers = new TimedWorker[numberOfWorkers];
for (int i = 0; i < workers.Length; i++)
{
workers[i] = new TimedWorker();
}
timer.Start();
while (totalWorkload[0] > 0)
{
foreach (var worker in workers)
{
totalWorkload[0] -= worker.Work();
}
}
return timer.ElapsedMilliseconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment