Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { int tmp = 0; for (int k = 0; k < size; k++) { tmp += m1[i, k] * m2[k, j]; } result[i, j] = tmp; } }
Parallel.For(0, size, (int i) => { for (int j = 0; j < size; j++) { int tmp = 0; for (int k = 0; k < size; k++) { tmp += m1[i, k] * m2[k, j]; } result[i, j] = tmp; } });
Task[] tasks = new Task[size]; for (int i = 0; i < size; i++) { tasks[i] = Task.Factory.StartNew(() => { for (int j = 0; j < size; j++) { int tmp = 0; for (int k = 0; k < size; k++) { tmp += m1[i, k] * m2[k, j]; } result[i, j] = tmp; } }); } Task.WaitAll(tasks);
Task[] tasks = new Task[size]; for (int n = 0; n < size; n++) { int i = n; tasks[n] = Task.Factory.StartNew(() => { for (int j = 0; j < size; j++) { int tmp = 0; for (int k = 0; k < size; k++) { tmp += m1[i, k] * m2[k, j]; } result[i, j] = tmp; } }); } Task.WaitAll(tasks);
Task[] tasks = new Task[size]; for (int i = 0; i < size; i++) { tasks[i] = Task.Factory.StartNew(ii => { int iii = (int)ii; for (int j = 0; j < size; j++) { int tmp = 0; for (int k = 0; k < size; k++) { tmp += m1[iii, k] * m2[k, j]; } result[iii, j] = tmp; } }, i); } Task.WaitAll(tasks);