我在一个类中编写了fork / join快速排序和正常的快速排序,然后我计算了执行时间。奇怪的是,fork / join的工作时间比普通的快速排序要长。问题是为什么?
public class Main
{
public static void main(String[] args)
{
Random generator = new Random();
List<Integer> list = new ArrayList<>(50000);
for(int i = 0; i < 50000; i ++)
{
list.add(generator.nextInt(500200));
}
List<Integer> list2 = new ArrayList<>(list);
ConcurrentQuickSort<Integer> sort = new ConcurrentQuickSort<>(list,0,list.size() - 1);
long startTime = System.currentTimeMillis();
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(sort);
pool.shutdown();
pool.awaitTermination(10,TimeUnit.SECONDS);
long endTime = System.currentTimeMillis();
long ConcurrentTime = (endTime - startTime);
startTime = System.currentTimeMillis();
ConcurrentQuickSort<Integer> sort2 = new ConcurrentQuickSort<>(list2,0,list.size() - 1);
sort2.doSort(0,list2.size() - 1);
endTime = System.currentTimeMillis();
long sequentialTime = (endTime - startTime);
System.out.println(ConcurrentTime);
System.out.println(sequentialTime);
System.out.println(sort2.isCorrect(list));
}
}
public class ConcurrentQuickSort<T extends Comparable<T> > extends RecursiveAction
{
private List<T> list;
private int size;
private int begin;
private int end;
public ConcurrentQuickSort(List<T> list,int begin,int end)
{
this.list = list;
this.size = this.list.size();
this.begin = begin;
this.end = end;
}
private int partition(int begin, int end)
{
T x = list.get(begin);
int i = begin;
int j = end;
while(true)
{
while(list.get(j).compareTo(x) > 0)
{
j--;
}
while(list.get(i).compareTo(x) < 0)
{
i++;
}
if(i < j)
{
swap(i,j);
i++;
j--;
}
else
return j;
}
}
public void doSort(int begin, int end)
{
int q;
if(begin < end)
{
q = partition(begin,end);
doSort(begin,q);
doSort(q + 1,end);
}
}
public void doSort()
{
}
public void sort()
{
doSort(0,list.size() - 1);
}
private void swap(int first, int second)
{
T temp = list.get(first);
list.set(first,list.get(second));
list.set(second,temp);
}
@Override
protected void compute()
{
int q;
if(begin < end)
{
q = partition(begin,end);
List<ConcurrentQuickSort<T>> subtasks = splitWork(q);
for(ConcurrentQuickSort<T> t : subtasks)
{
t.fork();
}
}
}
private List<ConcurrentQuickSort<T>> splitWork(int q)
{
List<ConcurrentQuickSort<T>> subtasks = new ArrayList<>();
subtasks.add(new ConcurrentQuickSort<>(this.list,begin,q));
subtasks.add(new ConcurrentQuickSort<>(this.list,q +1,end));
return subtasks;
}
public boolean isCorrect(List<T> other)
{
return list.equals(other);
}
}
执行正常快速排序的时间类似于20毫秒并且fork / join 80.为什么?
答案 0 :(得分:1)
对于足够小的数据集:
如果您实现了一个较低的阈值,您可以在并发和非并发快速排序之间以及快速排序和bubblesort之间切换,您可以计算出不同cpu-caches的行大小和算法。
RecursiveAction的javadoc显示了合并排序的实现,阈值设置为1000.