【提问帖】关系并行希尔排序的效率问题

有大佬能解释一下么,困扰了我一天了。。在《实战java高并发程序》上摘抄的一段关于并行shell并行和串行排序的代码,测试后发现并行排序在时间的消耗上要多余串行的时间消耗。讲道理的话,并行明显是要快于串行的速度的。以下是代码和测试结果 


// 普通希尔排序
public static void shellSort (int[] arr) {
    // 计算出h的最大值, h为间隔
    int h = 1;
    while (h <= arr.length / 3) {
        h = h * 3 + 1;
    }
    while (h > 0) {
        for (int i = h; i < arr.length; i++) {
            if (arr[i] < arr[i - h]) {
                int temp = arr[i];
                int j = i - h;
                while (j >= 0 && arr[j] > temp) {
                    arr[j + h] = arr[j];
                    j -= h;
                }
                arr[j + h] = temp;
            }
        }
        h = (h - 1) / 3;
    }
}
// 串行测试
public static void main(String[] args) {
    int[] arr = new int[200000];
    Random random = new Random();
    for (int i = 0; i < arr.length; i++) {
        arr[i] = random.nextInt();
    }
    long start = System.currentTimeMillis();
    shellSort(arr);
    System.out.println(System.currentTimeMillis() - start);
}

// 并行希尔排序

public class ShellSortConcur implements Runnable {
    static int i = 0;
    static int h = 0;
    static int[] arr;
    CountDownLatch countDownLatch;


    static ExecutorService pool = Executors.newCachedThreadPool();

    public ShellSortConcur(int i, int h, CountDownLatch countDownLatch) {
        this.i = i;
        this.h = h;
        this.countDownLatch = countDownLatch;
    }


    @Override
    public void run() {
        if (arr[i] < arr[ i - h]) {
            int temp = arr[i];
            int j = i - h;
            while (j >= 0 && arr[j] > temp) {
                arr[j + h] = arr[j];
                j -= h;
            }
            arr[j + h] = temp;
        }
        countDownLatch.countDown();
    }

    public static void shellSortConn () throws InterruptedException {
        int h = 1;
        CountDownLatch countDownLatch = null;
        while (h <= arr.length / 3) {
            h = h * 3 + 1;
        }
        while (h > 0) {
//                System.out.println("h = " + h);
            if (h >= 4) {
                countDownLatch = new CountDownLatch(arr.length - h);
            }
            for (int i = h; i < arr.length; i++) {
                if (h >= 4) {
                    pool.execute(new ShellSortConcur(i, h, countDownLatch));
                } else {
                    if (arr[i] < arr[ i - h]) {
                        int temp = arr[i];
                        int j = i - h;
                        while (j >= 0 && arr[j] > temp) {
                            arr[j + h] = arr[j];
                            j -= h;
                        }
                        arr[j + h] = temp;
                    }
                }
            }
            countDownLatch.await();
            h = (h - 1) / 3;
        }
    }
// 并行测试
    public static void main(String[] args) throws InterruptedException {
        arr = new int[200000];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt();
        }
        long start = System.currentTimeMillis();
        shellSortConn();
        System.out.println(System.currentTimeMillis() - start);

    }
}

 

到底是什么原因导致的呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值