Здравствуйте, Serginio1, Вы писали:
S> То максимальный прирост в 2 раза?
S> Ну это нормально. Там процессор еще на чтонибудь расходуется.
Я на компьютере, где это выполняется, ещё сижу из-под Тимвьюера. Вот ещё вариант. Страшненький, но побыстрее и постабильнее:
| C# |
| using System;
using System.Diagnostics;
using System.Threading;
namespace QuickSort
{
public class Program
{
private static int threadCount;
private static ManualResetEvent waitHandle = new ManualResetEvent(false);
public static void Main(string[] args)
{
const int ARRAY_SIZE = 1000 * 1000;
const int THREADS = 4;
var watch = new Stopwatch();
for (var i = 0; i < 5; i++)
{
watch.Restart();
waitHandle.Reset();
var vals = new TestClass[ARRAY_SIZE];
for (var j = 0; j < THREADS; j++)
{
int segmentIndex = j;
ThreadPool.QueueUserWorkItem(_ => Fill(vals, segmentIndex * (ARRAY_SIZE / THREADS), ARRAY_SIZE / THREADS));
}
waitHandle.WaitOne();
watch.Stop();
Console.WriteLine($"Total: {watch.ElapsedMilliseconds} ms");
}
Console.WriteLine($"GC: {GC.CollectionCount(0)} {GC.CollectionCount(1)} {GC.CollectionCount(2)}");
Console.ReadLine();
}
private static void Fill(TestClass[] array, int start, int count)
{
Interlocked.Increment(ref threadCount);
int end = start + count;
for (int i = start; i < end; i++)
{
array[i] = new TestClass();
}
Interlocked.Decrement(ref threadCount);
if (threadCount == 0)
{
waitHandle.Set();
}
}
}
internal class TestClass
{
public string Id = CreateGuid();
public string Value = CreateGuid();
private static readonly Random gen = new Random();
private static string CreateGuid() => gen.Next(100000000).ToString("0000000000");
}
}
|
| |
Total: 302 ms
Total: 262 ms
Total: 368 ms
Total: 359 ms
Total: 384 ms
GC: 109 41 7
Надеюсь, не накосячил.