static void sequential(Polynomial p1, Polynomial p2) { Polynomial.Builder builder = new Polynomial.Builder(); var watch = System.Diagnostics.Stopwatch.StartNew(); p1.polynomial .Zip(Enumerable.Range(0, p1.polynomial.Count), (a, b) => new { val = a, power = b }) .SelectMany(pair1 => p2.polynomial .Zip(Enumerable.Range(0, p2.polynomial.Count), (a, b) => new { val = a, power = b }) .Select(pair2 => new { First = pair1, Second = pair2 })) .ToList() .ForEach(pair => { var pwrX = pair.First.power + pair.Second.power; var n = pair.First.val * pair.Second.val; builder.of(pwrX, n); }); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; //builder.build().print(); Console.WriteLine($"Sequential Elapsed time: {elapsedMs} ms"); }
static void Main(string[] args) { //Polynomial p1 = new Polynomial.Builder() // .of(0, 5) // .of(1, 0) // .of(2, 10) // .of(3, 6) // .of(4, 1) // .build(); //Polynomial p2 = new Polynomial.Builder() // .of(0, 1) // .of(1, 2) // .of(2, 4) // .build(); var b1 = new Polynomial.Builder(); var b2 = new Polynomial.Builder(); for (int i = 0; i < 250; i++) { b1.of(i, i + 1); b2.of(i, 2 * i + 1); } Polynomial p1 = b1.build(); Polynomial p2 = b2.build(); sequential(p1, p2); parallel(p1, p2); seq_karatsuba(p1, p2); parallel_karatsuba(p1, p2); Console.ReadKey(); }
static async Task parallel_helper(List <int> p1, List <int> p2, int i, Polynomial.Builder builder) { for (int j = 0; j < p2.Count; j++) { var pwrX = i + j; var n = p1[i] * p2[j]; builder.of(pwrX, n); } }
static void parallel_karatsuba(Polynomial p1, Polynomial p2) { Polynomial.Builder builder = new Polynomial.Builder(); var watch = System.Diagnostics.Stopwatch.StartNew(); var res = karatsuba_aux_par(p1.polynomial, p2.polynomial).Result; watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; // builder.from(res).build().print(); Console.WriteLine($"Parallel Karatsuba Elapsed time: {elapsedMs} ms"); }
static async Task parallel(Polynomial p1, Polynomial p2) { Polynomial.Builder builder = new Polynomial.Builder(); var watch = System.Diagnostics.Stopwatch.StartNew(); for (int i = 0; i < p1.polynomial.Count; i++) { await parallel_helper(p1.polynomial, p2.polynomial, i, builder); } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; //builder.build().print(); Console.WriteLine($"Parallel Elapsed time: {elapsedMs} ms"); }