java几种排序方式速度的简单测试

在oschina上看到一篇排序速度测试的(http://my.oschina.net/nox/blog/489993?fromerr=W8001KYQ),但没有测试stream的速度.故增加该测试.

三种排序方式:

1.Collections.sort;

2.forkjoin;

3.stream sort.

上代码:

package sort;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.stream.Collectors;

public class GenerateSample {
	public static void main(String[] args) {
		writeFile();
		
		long t1 = System.currentTimeMillis();
		sort();
		long t2 = System.currentTimeMillis();
		System.out.println("sort total time=" + (t2-t1));
		forkJoin();
		long t3 = System.currentTimeMillis();
		System.out.println("forkjoin total time=" + (t3-t2));
		stream();
		long t4 = System.currentTimeMillis();
		System.out.println("stream total time=" + (t4-t3));
	}

	public static void writeFile() {
		File f = new File("/home/novelbio/tmp/sample.txt");
		FileWriter writer;
		try {
			writer = new FileWriter(f, false);
			Random random1 = new Random(10);
			for (int i = 0; i < 10000000; i++) {
				writer.write(String.valueOf(random1.nextInt(20000)));
				writer.write("\r\n");
			}

			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}
	}

	public static void sort() {
		File f = new File("/home/novelbio/tmp/sample.txt");
		List<Integer> arrayList = new ArrayList<>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(f));
			String str = null;
			while ((str = reader.readLine()) != null) {
				arrayList.add(Integer.valueOf(str));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		long startTime = System.currentTimeMillis();
		Collections.sort(arrayList);
		long endTime = System.currentTimeMillis();

		System.out.println("基本sort排序所花时间:" + (endTime - startTime) + "ms");

		File f2 = new File("/home/novelbio/tmp/original_sorted.txt");
		FileWriter writer2;
		try {
			writer2 = new FileWriter(f2, false);

			for (int i = 0; i < arrayList.size(); i++) {
				writer2.write(String.valueOf(arrayList.get(i)));
				writer2.write("\r\n");
			}

			writer2.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}
	}

	public static void forkJoin() {
		File f = new File("/home/novelbio/tmp/sample.txt");
		List<Integer> arrayList = new ArrayList<>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(f));
			String str = null;
			while ((str = reader.readLine()) != null) {
				arrayList.add(Integer.valueOf(str));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		long longArray[] = new long[arrayList.size()];
		for (int i = 0; i < arrayList.size(); i++) {
			longArray[i] = Long.parseLong(arrayList.get(i).toString());
		}

		ForkJoinPool pool = new ForkJoinPool();

		FastSort fastSort = new FastSort(longArray);

		long startTime = System.currentTimeMillis();
		pool.execute(fastSort);
		while (!fastSort.isDone()) {

		}

		long endTime = System.currentTimeMillis();

		System.out.println("fockJoin排序所花时间:" + (endTime - startTime) + "ms");
		File f2 = new File("/home/novelbio/tmp/fockJoinSorted.txt");

		FileWriter writer2;
		try {
			writer2 = new FileWriter(f2, false);

			for (int i = 0; i < longArray.length; i++) {
				writer2.write(String.valueOf(longArray[i]));
				writer2.write("\r\n");
			}

			writer2.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}

	}
	
	
	public static void stream() {
		File f = new File("/home/novelbio/tmp/sample.txt");
		List<Integer> arrayList = new ArrayList<>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(f));

			String str = null;
			while ((str = reader.readLine()) != null) {
				arrayList.add(Integer.valueOf(str));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}



		long startTime = System.currentTimeMillis();
		
		ArrayList<Integer> newlist = (ArrayList) arrayList.stream().sorted((p1, p2) -> (p1 - p2)).collect(Collectors.toList());

		long endTime = System.currentTimeMillis();

		System.out.println("streamSorted排序所花时间:" + (endTime - startTime) + "ms");
		File f2 = new File("/home/novelbio/tmp/streamSorted.txt");

		FileWriter writer2;
		try {
			writer2 = new FileWriter(f2, false);

			for (Integer i : newlist) {
				writer2.write(String.valueOf(i));
				writer2.write("\r\n");
			}

			writer2.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

		}

	}

}

class FastSort extends RecursiveAction {

	private static final long serialVersionUID = 1L;
	
	final long[] array;
	final int lo;
	final int hi;
	private int THRESHOLD = 30;

	public FastSort(long[] array) {
		this.array = array;
		this.lo = 0;
		this.hi = array.length - 1;
	}

	public FastSort(long[] array, int lo, int hi) {
		this.array = array;
		this.lo = lo;
		this.hi = hi;
	}

	protected void compute() {
		if (hi - lo < THRESHOLD)
			sequentiallySort(array, lo, hi);
		else {
			int pivot = partition(array, lo, hi);
			FastSort left = new FastSort(array, lo, pivot - 1);
			FastSort right = new FastSort(array, pivot + 1, hi);

			invokeAll(left, right);
		}
	}

	private int partition(long[] array, int lo, int hi) {
		long x = array[hi];
		int i = lo - 1;
		for (int j = lo; j < hi; j++) {
			if (array[j] <= x) {
				i++;
				swap(array, i, j);
			}
		}
		swap(array, i + 1, hi);
		return i + 1;
	}

	private void swap(long[] array, int i, int j) {
		if (i != j) {
			long temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
	}

	private void sequentiallySort(long[] array, int lo, int hi) {
		Arrays.sort(array, lo, hi + 1);
	}
}

 

测试结果如下(每人机器配置不同,花费时间会有不同.):

基本sort排序所花时间:3627ms
sort total time=8176
fockJoin排序所花时间:1345ms
forkjoin total time=5846
streamSorted排序所花时间:2581ms
stream total time=5566

基本结论:

 

基本sort(3627ms)>streamSorted(2581ms)>fockJoin(1345ms)

 

 

 

 

你可能感兴趣的:(java,排序,Stream,sort,速度)