并行快速排序不比顺序快速排序快

Parallel quick sort not running faster than sequential quick sort

本文关键字:快速排序 顺序 并行      更新时间:2023-10-16

我的QuickSort工作正常,它可以正确输出,但是并行版本的执行速度不超过非并行版本。我还能做什么使其运行速度更快?

void quickSort(int arr[], int low, int high)
{
    int pi;
    if (low < high)
    {
        //pi is partitioning index, arr[p] is now at right place 
        pi = partition(arr, low, high);
        // Separately sort elements before partition and after partition
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}
void quickSort_parallel_omp(int arr[], int low, int high)
{
    int pi;
    if (low < high)
    { 
        pi = partition(arr, low, high);
        omp_set_nested(1);
        #pragma omp parallel sections num_threads(Nthreads)
        {           //Nthreads is declared in the main as int Nthreads = 4
            #pragma omp section
            quickSort_parallel_omp(arr, low, pi - 1);
            #pragma omp section
            quickSort_parallel_omp(arr, pi + 1, high);
        }
    }
}

可能是将数据分配到多个并行处理单元中的开销并再次将结果组合起来超过了由于代码并行而导致的性能增益。我建议您增加输入大小,以查看可观的性能增长。

在您的情况下,创建线程的时间,将内存和时间切成o/s分配给每个线程的时间比简短数据所花费的时间更多。

仅在处理大量数据时才可以看到并行处理的性能。此外,在多处理或并行处理中还需要考虑许多其他因素,例如处理器的系统内存数量等。您可以在此主题上获得大量的在线文档。