public static List <long> GetPrimesParallel(long first, long last) { var lockObject = new object(); IEnumerable <long> result = new List <long>(); Parallel.ForEach( Partitioner.Create(first, last), () => new List <long>(), (range, loopState, partialResult) => { for (long i = range.Item1; i < range.Item2; i++) { if (PrimeTool.IsPrime(i)) { partialResult.Add(i); } } return(partialResult); }, (partialResult) => { lock (lockObject) { result = result.Concat(partialResult); } } ); return(result.OrderBy(s => s).ToList()); }
public void TestRange(long first, long last) { Console.WriteLine("Testing range {0} - {1}", first, last); Console.Write(" Sequential:"); MeasureTime(new Action(() => PrimeTool.GetPrimesSequential(first, last))); Console.Write(" Parallel: "); MeasureTime(new Action(() => PrimeTool.GetPrimesParallel(first, last))); Console.WriteLine(); }
public void CheckCorrectness() { var s = PrimeTool.GetPrimesSequential(1, 40); var p = PrimeTool.GetPrimesParallel(1, 40); for (int i = 0; i < s.Count; i++) { Console.WriteLine("{0} == {1}", s[i], p[i]); } Console.WriteLine(); }
private async void PrimesButtonSequ(object sender, EventArgs e) { this.primeList = await PrimeTool.GetPrimesSequentialAsync(Convert.ToInt64(inputStart.Value), Convert.ToInt64(inputEnd.Value)); this.PopulateListBox(); }
public async void WarmUp() { var s = PrimeTool.GetPrimesSequential(1, 5); var p = await PrimeTool.GetPrimesParallelAsync(1, 5); }