using System.Diagnostics;
var tab = CréerTableau(N * N);
var (r0, dt0) = Tester(() => CompterMT(1, tab));
Console.WriteLine($"1 fil : compté {r0} impairs en {dt0} ms");
var (r1, dt1) = Tester(() => CompterMT(2, tab));
Console.WriteLine($"2 fils : compté {r1} impairs en {dt1} ms");
var (r2, dt2) = Tester(() => CompterMT(4, tab));
Console.WriteLine($"4 fils : compté {r2} impairs en {dt2} ms");
var (r3, dt3) = Tester(() => CompterMT(8, tab));
Console.WriteLine($"8 fils : compté {r3} impairs en {dt3} ms");
static short[] CréerTableau(int n)
short[] tab = new short[n];
for (int i = 0; i != tab.Length; ++i)
tab[i] = (short)(i * 2 + 1);
static int CompterSi<T>(T[] tab, Func<T, bool> pred, int début, int fin)
for (int i = début; i != fin; ++i)
static (T rés, long dt) Tester<T>(Func<T> f)
var sw = new Stopwatch();
return (rés, sw.ElapsedMilliseconds);
static int CompterMT(int nbThreads, short [] tab)
int[] nbImpairs = new int[nbThreads];
int tailleBloc = tab.Length / nbThreads;
Thread[] thrs = new Thread[nbThreads - 1];
for(int i = 0; i != thrs.Length; ++i)
int début = index * tailleBloc;
int fin = début + tailleBloc;
thrs[index] = new Thread(() =>
for (int j = début; j != fin; ++j)
foreach (var th in thrs) th.Start();
int début = (nbThreads - 1) * tailleBloc;
for (int j = début; j != fin; ++j)
++nbImpairs[nbThreads - 1];
foreach (var th in thrs) th.Join();
foreach (int n in nbImpairs)