编程珠玑:10^7个正整数文件排序

昨天看《编程珠玑》看见这个问题,又恰好自学Java,看到《Java编程思想》中的IO章节,就分别用Java中的io和nio方式将其位图结构排序实现了下

PS:没有按照它的内存约束要求

位图结构排序:

package Pearls;
import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

abstract class Tester{
	static final int DATASIZE=10000000;
	String name;
	Tester(String name){
		this.name=name;
	}
	
	public void runTest() throws Exception{
		long startTime = System.currentTimeMillis();  
		test();
		long endTime = System.currentTimeMillis(); 
		
		//输出使用方法和运行时间
		System.out.println(name+":\n程序运行时间:" + (endTime - startTime) + "ms");
		//内存使用情况查看,不知这样查看对不对,网上找的
		Runtime rt = Runtime.getRuntime();
		long total_mem = rt.totalMemory();
		long free_mem = rt.freeMemory();
		long used_mem = total_mem - free_mem;
		System.out.println("Amount of used memory: " + used_mem/1024/1024+"M");
	}
	
	abstract void test() throws IOException;
}

public class BitSort {
	
	                                                                                                                     
	public static void main(String[] args) throws Exception  {
		Tester f1=new Tester("Buffered"){
		 	public void test() throws IOException {
				//位图设置
		 		BitSet bm=new BitSet(DATASIZE);  
				bm.clear();
				
				//数据输入设置
				DataInputStream br= new DataInputStream(new BufferedInputStream(new FileInputStream("./src/Pearls/data.txt")));
				
		        for(int i=0;i<DATASIZE;i++){
		        	bm.set(br.readInt(), true);
		        }
		        br.close();
		        
				FileOutputStream fw=new FileOutputStream("./src/Pearls/datas.txt");
				DataOutputStream dw=new DataOutputStream(new BufferedOutputStream(fw));
				for(int i=0;i<DATASIZE;i++){
		        	if(bm.get(i))
		        		dw.writeInt(i);
		        }
				dw.close();
			}
		};
		f1.runTest();
		
	    Tester f2=new Tester("Mapped"){
	    	public void test() throws IOException{
	    		//位图设置
		 		BitSet bm=new BitSet(DATASIZE);  
				bm.clear();
		        FileChannel fc=new FileInputStream("./src/Pearls/data.txt").getChannel();
		        IntBuffer ib=fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
		        long size=fc.size();
	            for(int i=0;i<DATASIZE;i++){
	        	    bm.set(ib.get(), true);
	            }
	            fc.close();	        
			    
                            FileChannel fc2=new RandomAccessFile("./src/Pearls/data2s.txt","rw")
			                           .getChannel();
			    IntBuffer ibw=fc2.map(FileChannel.MapMode.READ_WRITE,0,size)
					             .asIntBuffer();
			    for(int i=0;i<DATASIZE;i++){
	        	    if(bm.get(i))
	        		    ibw.put(i);
	            }
			    fc2.close();

	    	}
	    };
	    f2.runTest();
		
	}

}
输出结果:

Buffered:
程序运行时间:3820ms
Amount of used memory: 3M
Mapped:
程序运行时间:189ms
Amount of used memory: 4M

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值