java硬盘存储,Java - 存储和上传数组到/从内存到磁盘

I have an int and float array of length 220 million (fixed). Now, I want to store/upload those arrays to/from memory and disk. Currently, I am using Java NIO's FileChannel and MappedByteBuffer to solve this. It works fine, but takes near about 5 seconds (Wall Clock Time) for storing/uploading array to/from memory to disk. Actually, I want more faster one. Can anybody help me, is there any inbuilt java library/ database / any other approach to make uploading/storing arrays much faster ? I specially care about uploading to memory from disk. I want to make it faster. So, if storing time will increase to do that I have no issue. Thanks in advance.

The code I am using is given below (if required):

int savenum = 220000000 ;

public void save() {

try {

long l = 0 ;

FileChannel channel = new RandomAccessFile(str1, "rw").getChannel();

MappedByteBuffer mbb = channel.map(FileChannel.MapMode.READ_WRITE, 0, savenum * 8);

mbb.order(ByteOrder.nativeOrder());

for(int i = 0 ; i < savenum ; i++){

l = a[i] ;

mbb.putLong(l);

}

channel.close();

FileChannel channel1 = new RandomAccessFile(str2, "rw").getChannel();

MappedByteBuffer mbb1 = channel1.map(FileChannel.MapMode.READ_WRITE, 0, savenum * 4);

mbb1.order(ByteOrder.nativeOrder());

for(int i = 0 ; i < savenum ; i++){

int ll = b[i] ;

mbb1.putInt(ll);

}

channel1.close();

}

catch (Exception e){

System.out.println("IOException : " + e);

}

}

public void load(){

try{

FileChannel channel2 = new RandomAccessFile(str1, "r").getChannel();

MappedByteBuffer mbb2 = channel2.map(FileChannel.MapMode.READ_ONLY, 0, channel2.size());

mbb2.order(ByteOrder.nativeOrder());

assert mbb2.remaining() == savenum * 8;

for (int i = 0; i < savenum; i++) {

long l = mbb2.getLong();

a[i] = l ;

}

channel2.close();

FileChannel channel3 = new RandomAccessFile(str2, "r").getChannel();

MappedByteBuffer mbb3 = channel3.map(FileChannel.MapMode.READ_ONLY, 0, channel3.size());

mbb3.order(ByteOrder.nativeOrder());

assert mbb3.remaining() == savenum * 4;

for (int i = 0; i < savenum; i++) {

int l1 = mbb3.getInt();

b[i] = l1 ;

}

channel3.close();

}

catch(Exception e){

System.out.println(e) ;

}

}

解决方案

If you want to speed up the operation, you can change your code so you are not doing the copy at all. i.e. use the ByteBuffer, or IntBuffer or LongBuffer. This has the benefit of saving a copy into heap of what you off heap already, but also you only load as you use it. i.e. your processing can be concurrent with the loading.

Using this approach should cut your initial "load" time to around 10 ms, and there is no "save" time because it will already be available to the OS.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值