我知道之前已经问过这个问题,我找到的答案都是关于先发制人的问题。同步开销等。但是,我仍然很想知道我自己的情况的答案。所以这就是这笔交易。
我在Intel Core i7-2670QM CPU上运行(4个内核,8个线程),我编写了这段代码:
using System;
using System.Diagnostics;
using System.Threading;
namespace _T
{
class Program
{
private static void stquicksort(object parameter)
{
object[] parameters = (object[])parameter;
int[] array = (int[])parameters[0];
int left = (int)parameters[1];
int right = (int)parameters[2];
if (left >= right) return;
int temp = (left + right) / 2;
int pivot = array[temp];
array[temp] = array[right];
int j = left;
for (int i = left; i < right; i++)
{
if (array[i] < pivot)
{
if (i != j)
{
temp = array[i];
array[i] = array[j];
array[j++] = temp;
}
else j++;
}
}
array[right] = array[j];
array[j] = pivot;
stquicksort(new object[] { array, left, j - 1 });
stquicksort(new object[] { array, j + 1, right });
}
private static void mtquicksort(object parameter)
{
object[] parameters = (object[])parameter;
int[] array = (int[])parameters[0];
int left = (int)parameters[1];
int right = (int)parameters[2];
if (left >= right) return;
int temp = (left + right) / 2;
int pivot = array[temp];
array[temp] = array[right];
int j = left;
for (int i = left; i < right; i++)
{
if (array[i] < pivot)
{
if (i != j)
{
temp = array[i];
array[i] = array[j];
array[j++] = temp;
}
else j++;
}
}
array[right] = array[j];
array[j] = pivot;
Thread t = new Thread(mtquicksort);
t.Start(new object[] { array, left, j - 1 });
mtquicksort(new object[] { array, j + 1, right });
t.Join();
}
private static void dump(int[] array)
{
Console.Write("Array:");
foreach (int el in array) Console.Write(" " + el);
Console.WriteLine();
}
private static void Main(string[] args)
{
while (true)
{
Console.Write("Enter the number of elements: ");
int count = Convert.ToInt32(Console.ReadLine());
if (count < 0) break;
Random rnd = new Random();
int[] array1 = new int[count];
for (int i = 0; i < array1.Length; i++)
array1[i] = rnd.Next(1, 100);
int[] array2 = (int[])array1.Clone();
Stopwatch sw = new Stopwatch();
sw.Reset(); sw.Start();
stquicksort(new object[] { array1, 0, array1.Length - 1 });
sw.Stop();
Console.WriteLine("[ST] Time needed: " + sw.ElapsedMilliseconds + "ms");
sw.Reset(); sw.Start();
mtquicksort(new object[] { array2, 0, array2.Length - 1 });
sw.Stop();
Console.WriteLine("[MT] Time needed: " + sw.ElapsedMilliseconds + "ms");
}
Console.WriteLine("Press any key to exit . . .");
Console.ReadKey(true);
}
}
}
stquicksort是单线程,mtquicksort是多线程的,是的,我有意地保留了st参数,因此两个版本的装箱/拆箱开销是相同的(如果有任何明显的话)。我已将解决方案置于发布状态(禁用所有调试),输出有点令人伤心:
Enter the number of elements: 100
[ST] Time needed: 0ms
[MT] Time needed: 323ms
Enter the number of elements: 1000
[ST] Time needed: 0ms
[MT] Time needed: 7476ms
Enter the number of elements: 1000
[ST] Time needed: 0ms
[MT] Time needed: 7804ms
Enter the number of elements: 1000
[ST] Time needed: 0ms
[MT] Time needed: 7474ms
Enter the number of elements: 10
[ST] Time needed: 0ms
[MT] Time needed: 32ms
Enter the number of elements: 100
[ST] Time needed: 0ms
[MT] Time needed: 339ms
再次,问题是先发制人,是否可能是代码中的缺陷?更重要的是,什么是解决这个问题的正确方法。
答案 0 :(得分:1)
产生线程是一项相当昂贵的操作。它不是瞬间的,所以你看到的大量时间不是执行排序所需的额外时间,而是产生踏板所需的时间。当你生成一个新线程以使它值得它时,该线程必须运行一段时间。
.NET和C#确实有一个任务系统任务类似于线程,除了它们在线程池上运行而不是每次都产生一个新线程。这使您可以进行多线程任务,而无需为每个任务创建新线程的高成本。
尝试用此替换您的线程代码。
Task t = Task.Run(()=>mtquicksort(new object[] { array, left, j - 1 }));
t.Wait();
请注意,您必须使用System.Threading.Tasks命名空间