And the difference is huge, up to 10 times!
On the 4-core processor, the program below with a low proiritet process runs from 800 ms to 1600, while with a high - was not less than 9 seconds
How to explain this unexpected behavior?
Ps. Changed the priority in the task manager, checked many times in the Release configuration without VS, Windows 10
using System; using System.Diagnostics; using System.Threading; namespace LoadTest { class Test { const int n = 1000; static int count = 0; const int stepCount = 10; static AutoResetEvent are = new AutoResetEvent(false); static ManualResetEvent mre = new ManualResetEvent(false); static AutoResetEvent areEnd = new AutoResetEvent(false); static void Main(string[] args) { while (true) { char action = Console.ReadKey().KeyChar; switch (action) { case 't': Threads(); break; } are.Reset(); mre.Reset(); areEnd.Reset(); } } static void Threads() { Thread[] ts = new Thread[n]; for (int i = 0; i < n; i++) { ts[i] = new Thread(CPU); ts[i].Start(); } are.WaitOne();//все потоки готовы var sw = Stopwatch.StartNew(); mre.Set();//начать расчет areEnd.WaitOne();//расчет окончен sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); } static void CPU(object i) { Interlocked.Increment(ref count); if (count == n) are.Set();//оповещаем что все потоки созданы mre.WaitOne();//ждем создания всех потоков Simple(); Interlocked.Decrement(ref count); if (count == 0) areEnd.Set();//оповещаем о завершении всех потоков } static void Simple() { for (int j = 0; j < stepCount; j++) Console.WriteLine(j); } } }