用ForkJoin框架为归并排序提速

前段时间刷leetcode,时隔12年重新刷了一遍归并排序的题目(上一次是读大学的时候)。归并排序属于典型的分治思想的算法。每层递归有三个步骤

  1. 分解(Divide):将n个元素分成个含n/2个元素的子序列。
  2. 解决(Conquer):用合并排序法对两个子序列递归的排序。
  3. 合并(Combine):合并两个已排序的子序列已得到排序结果。

使用递归算法的归并排序代码demo

public class SortArray {
	
	int[] tmp;
	
	public int[] sortArray(int[] nums) {
		tmp = new int[nums.length];
		mergeSort(nums, 0, nums.length - 1);
		return nums;
	}
	
	private void mergeSort(int[] nums, int left, int right) {
		if (left >= right) {
			return;
		}
		
		int middle = (left + right) / 2;
		
		mergeSort(nums, left, middle);
		int newLeft = middle + 1;
		mergeSort(nums, newLeft, right);
		
		int i = left, j = newLeft;
		int k = 0;
		while (i <= middle && j <= right) {
			if (nums[i] < nums[j]) {
				tmp[k++] = nums[i++];
			} else {
				tmp[k++] = nums[j++];
			}
		}
		
		while (i <= middle) {
			tmp[k++] = nums[i++];
		}
		while (j <= right) {
			tmp[k++] = nums[j++];
		}
		
		for (i = 0; i < k; i++) {
			nums[left + i] = tmp[i];
		}
	}
}

写完后,想到这个程序仅仅利用到了一个cpu核心,如果数据量很大的情况下,会造成计算资源的浪费。这个需要计算多个子任务的分治算法,明显可以用Fork/Join框架提速。于是改写了一版。当数据量小于1024个的时候不再进行切割,否则任务太多,提速效果十分差。当小于1024个的时候,偷懒用了工具库的快排。源码地址:https://github.com/bruce256/LeetCodeOJ/blob/master/src/main/java/divideAndConquer/SortArrayTask.java

public class SortArrayTask extends RecursiveTask<int[]> {
	
	public static final int THRESHOLD = 1024;
	
	int[] nums;
	int   left;
	int   right;
	int[] tmp;
	
	public SortArrayTask(int[] nums, int[] tmp, int left, int right) {
		this.nums  = nums;
		this.left  = left;
		this.right = right;
		this.tmp   = tmp;
	}
	
	@Override
	protected int[] compute() {
		if (right - left + 1 <= THRESHOLD) {
			Arrays.sort(nums, left, right + 1);
			return null;
		}
		
		int middle = (left + right) / 2;
		
		// 当前任务纳入计算队列
		SortArrayTask leftSortArrayTask = new SortArrayTask(nums, tmp, left, middle);
		leftSortArrayTask.fork();
		
		int           newLeft            = middle + 1;
		SortArrayTask rightSortArrayTask = new SortArrayTask(nums, tmp, newLeft, right);
		rightSortArrayTask.fork();
		
		// 等待任务计算结束,再做这个语句后的事情
		leftSortArrayTask.join();
		rightSortArrayTask.join();
		
		int i = left, j = newLeft;
		int k = 0;
		while (i <= middle && j <= right) {
			if (nums[i] < nums[j]) {
				tmp[k++] = nums[i++];
			} else {
				tmp[k++] = nums[j++];
			}
		}
		
		while (i <= middle) {
			tmp[k++] = nums[i++];
		}
		while (j <= right) {
			tmp[k++] = nums[j++];
		}
		
		for (i = 0; i < k; i++) {
			nums[left + i] = tmp[i];
		}
		return nums;
	}
	
	public static void main(String[] args) {
		int[] array = {10000, 100000, 1000000, 10000000, 100000000};
		for (int num : array) {
			compare(num);
		}
	}
	
	private static void compare(int num) {
		int[]  nums   = new int[num];
		int[]  tmp    = new int[num];
		Random random = new Random();
		for (int i = 0; i < nums.length; i++) {
			nums[i] = random.nextInt();
		}
		System.out.println(nums.length + " numbers \t" + Runtime.getRuntime().availableProcessors() + " cpus");
		
		ForkJoinPool  forkJoinPool = new ForkJoinPool();
		SortArrayTask task         = new SortArrayTask(nums, tmp, 0, nums.length - 1);
		long          start        = System.currentTimeMillis();
		Future<int[]> result       = forkJoinPool.submit(task);
		try {
			int[] r = result.get();
			
			long duration = System.currentTimeMillis() - start;
			System.out.println("fork/join time cost: \t" + duration + " ms");
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		SortArray test = new SortArray();
		for (int i = 0; i < nums.length; i++) {
			nums[i] = random.nextInt();
		}
		start = System.currentTimeMillis();
		int[] array    = test.sortArray(nums);
		long  duration = System.currentTimeMillis() - start;
		System.out.println("single thread time cost: \t" + duration + " ms");
	}
}

我的2015款MBP是i7 4核心8线程,跑了10000, 100000, 1000000, 10000000, 100000000个数据对别单线程和多线程版本,运行结果

10000 numbers 	8 cpus
fork/join time cost: 	5 ms
single thread time cost: 	4 ms

100000 numbers 	8 cpus
fork/join time cost: 	29 ms
single thread time cost: 	29 ms

1000000 numbers 	8 cpus
fork/join time cost: 	80 ms
single thread time cost: 	142 ms

10000000 numbers 	8 cpus
fork/join time cost: 	235 ms
single thread time cost: 	1725 ms

100000000 numbers 	8 cpus
fork/join time cost: 	2284 ms
single thread time cost: 	17275 ms

换2021款的14寸mbp,10核心的M1PRO芯片,16GB内存,运行

10000 numbers 	10 cpus
fork/join time cost: 	5 ms
single thread time cost: 	1 ms

100000 numbers 	10 cpus
fork/join time cost: 	32 ms
single thread time cost: 	20 ms

1000000 numbers 	10 cpus
fork/join time cost: 	81 ms
single thread time cost: 	94 ms

10000000 numbers 	10 cpus
fork/join time cost: 	98 ms
single thread time cost: 	1035 ms

100000000 numbers 	10 cpus
fork/join time cost: 	1041 ms
single thread time cost: 	11769 ms

2023款16寸mbp, M2PRO,16GB内存

10000 numbers 	12 cpus
fork/join time cost: 	4 ms
single thread time cost: 	2 ms

100000 numbers 	12 cpus
fork/join time cost: 	11 ms
single thread time cost: 	18 ms

1000000 numbers 	12 cpus
fork/join time cost: 	75 ms
single thread time cost: 	85 ms

10000000 numbers 	12 cpus
fork/join time cost: 	80 ms
single thread time cost: 	947 ms

100000000 numbers 	12 cpus
fork/join time cost: 	824 ms
single thread time cost: 	10920 ms
总结
  1. 当数据量较小时,由于CPU上下文切换,导致并行还不如串行快。当数据量较大时,提速明显。
  2. 当数据量达到1亿,并行提速比能超过cpu的核心数,应该是当数据量小于1024时使用的快排导致。当数据十分随机的时候,快排能达到O(n)的复杂度。

思考题:快排能不能用fork/join提速,堆排序呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bruce128

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值