来源:
https://www.cnblogs.com/xupengzhang/p/7966755.html
问题
给你1个文件bigdata,大小4663M,5亿个数,文件中的数据随机,如下一行一个整数:
61963023557681612158020393452095006174677379343122016371712330287901712966901...7005375
现在要对这个文件进行排序,怎么搞?
内部排序
先尝试内排,选2种排序方式:
private final int cutoff = 8;
public void perform(Comparable[] a) {
perform(a,0,a.length - 1);
}
private int median3(Comparable[] a,int x,int y,int z) {
if(lessThan(a[x],a[y])) {
if(lessThan(a[y],a[z])) {
return y;
}
else if(lessThan(a[x],a[z])) {
return z;
}else {
return x;
}
}else {
if(lessThan(a[z],a[y])){
return y;
}else if(lessThan(a[z],a[x])) {
return z;
}else {
return x;
}
}
}
private void perform(Comparable[] a,int low,int high) {
int n = high - low + 1;
//当序列非常小,用插入排序
if(n <= cutoff) {
InsertionSort insertionSort = SortFactory.createInsertionSort();
insertionSort.perform(a,low,high);
//当序列中小时,使用median3
}else if(n <= 100) {
int m = median3(a,low,low + (n >>> 1),high);
exchange(a,m,low);
//当序列比较大时,使用ninther
}else {
int gap = n >>> 3;
int m = low + (n >>> 1);
int m1 = median3(a,low,low + gap,low + (gap << 1));
int m2 = median3(a,m - gap,m,m + gap);
int m3 = median3(a,high - (gap << 1),high - gap,high);
int
ninther = median3(a,m1,m2,m3);
exchange(a,ninther,low);
}
if(high <= low)
return;
//lessThan
int lt = low;
//greaterThan
int gt = high;
//中心点
Comparablepivot = a[low];
int i = low + 1;
/*
* 不变式:
* a[low..lt-1] 小于pivot -> 前部(first)
* a[lt..i-1] 等于 pivot -> 中部(middle)
* a[gt+1..n-1] 大于 pivot -> 后部(final)
*
* a[i..gt] 待考察区域
*/
while (i <= gt) {
if(lessThan(a[i],pivot)) {
//i-> ,lt ->
exchange(a,lt++,i++);
}else if(lessThan(pivot,a[i])) {
exchange(a,i,gt--);
}else{
i++;
}
}
// a[low..lt-1] < v = a[lt..gt] < a[gt+1..high].
perform(a,low,lt -
1);
perform(a,gt + 1,high);
}
归并排序:
/**
* 小于等于这个值的时候,交给插入排序
*/
private final int cutoff = 8;
/**
* 对给定的元素序列进行排序
*
* @param a 给定元素序列
*/
@Override
public void perform(Comparable[] a) {
Comparable[] b = a.clone();
perform(b, a, 0, a.length - 1);
}
private void perform(Comparable[] src,Comparable[] dest,int low,int high) {
if(low >= high)
return;
//小于等于cutoff的时候,交给插入排序
if(high - low <= cutoff) {
SortFactory.createInsertionSort().perform(dest,low,high);
return;
}
int mid = low + ((high - low) >>> 1);
perform(dest,src,low,mid);
perform(dest,src,mid + 1,high);
//考虑局部有序 src[mid] <= src[mid+1]
if(lessThanOrEqual(src[mid],src[mid+1])) {
System.arraycopy(src,low,dest,low,high - low + 1);
}
//src[low .. mid] + src[mid+1 .. high] -> dest[low .. high]
merge(src,dest,low,mid,high);
}
private void merge(Comparable[] src,Comparable[] dest,int low,int mid,int high) {
for(int i = low,v = low,w = mid + 1; i <= high; i++) {
if(w > high || v <= mid && lessThanOrEqual(src[v],src[w])) {
dest[i] = src[v++];
}else {
dest[i] = src[w++];
}
}
}
数据太多,递归太深 ->栈溢出?加大Xss?数据太多,数组太长 -> OOM?加大Xmx?
耐心不足,没跑出来.而且要将这么大的文件读入内存,在堆中维护这么大个数据量,还有内排中不断的拷贝,对栈和堆都是很大的压力,不具备通用性。
sort命令来跑
跑了多久呢?24分钟.
为什么这么慢?
粗略的看下我们的资源:
内存 jvm-heap/stack,native-heap/stack,page-cache,block-buffer 外存 swap + 磁盘 数据量很大,函数调用很多,系统调用很多,内核/用户缓冲区拷贝很多,脏页回写很多,io-wait很高,io很繁忙,堆栈数据不断交换至swap,线程切换很多,每个环节的锁也很多.
总之,内存吃紧,问磁盘要空间,脏数据持久化过多导致cache频繁失效,引发大量回写,回写线程高,导致cpu大量时间用于上下文切换,一切,都很糟糕,所以24分钟不细看了,无法忍受.
位图法
private BitSet bits;
public void perform(
String largeFileName,
int total,
String destLargeFileName,
Castor<Integer> castor,
int readerBufferSize,
int writerBufferSize,
boolean asc) throws IOException {
System.out.println("BitmapSort Started.");
long start = System.currentTimeMillis();
bits = new BitSet(total);
InputPart<Integer> largeIn = PartFactory.createCharBufferedInputPart(largeFileName, readerBufferSize);
OutputPart<Integer> largeOut = PartFactory.createCharBufferedOutputPart(destLargeFileName, writerBufferSize);
largeOut.delete();
Integer data;
int off = 0;
try {
while (true) {
data = largeIn.read();
if (data == null)
break;
int v = data;