I am puzzled why setting GCSettings.LatencyMode
to GCLatencyMode.LowLatency
negatively impacts the time of execution?
Please consider the following code. Note that I have sufficient threads in the thread pool so I ensure there is no latency introduced here. Also, I have plenty of memory available on this machine. The difference between running in Interactive
and LowLatency
causes a 3 fold increase in execution time for LowLatency
.
class Program
{
static void Main(string[] args)
{
//capture current latency mode
var currentLatencyMode = GCSettings.LatencyMode;
//set low latency mode to minimize garbage collection
GCSettings.LatencyMode = GCLatencyMode.LowLatency;
var watch = new Stopwatch();
var numberTasksToSpinOff = 4;
var numberItems = 20000;
var random = new Random((int)DateTime.Now.Ticks);
var dataPoints = Enumerable.Range(1, numberItems).Select(x => random.NextDouble()).ToList();
var workers = new List<Worker>();
//structure workers
for (int i = 1; i <= numberTasksToSpinOff; i++)
{
workers.Add(new Worker(i, dataPoints));
}
//start timer
watch.Restart();
//parallel work
if (workers.Any())
{
var processorCount = Environment.ProcessorCount;
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = processorCount };
Parallel.ForEach(workers, parallelOptions, DoSomeWork);
}
//stop timer
watch.Stop();
//reset latency mode
GCSettings.LatencyMode = currentLatencyMode;
Console.WriteLine($"Time it took to complete in Milliseconds: {watch.ElapsedMilliseconds}");
Console.WriteLine("Press key to quit");
Console.ReadLine();
}
private static void DoSomeWork(Worker worker)
{
Console.WriteLine($"WorkerId: {worker.WorkerId} -> New Tasks spun off with in Thread Id: {Thread.CurrentThread.ManagedThreadId}");
var indexPos = 0;
foreach (var dp in worker.DataPoints)
{
var subset = worker.DataPoints.Skip(indexPos).Take(worker.DataPoints.Count - indexPos).ToList();
indexPos++;
}
}
}
public class Worker
{
public int WorkerId { get; set; }
public List<double> DataPoints { get; set; }
public Worker(int workerId, List<double> dataPoints)
{
WorkerId = workerId;
DataPoints = dataPoints;
}
}