Java - 存储和上传数组到/从内存到磁盘 [英] Java - Store and Upload Arrays to/from Memory to Disk

查看:196
本文介绍了Java - 存储和上传数组到/从内存到磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个int和float数组长度为220万(固定)。现在,我想存储/上传这些数组到/从内存和磁盘。目前,我使用Java NIO的FileChannel和MappedByteBuffer来解决这个问题。它工作正常,但大约5秒(墙上时钟时间)用于存储/上传阵列到/从内存到磁盘。其实,我想要更快一个。任何人都可以帮助我,有没有任何内置的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) ;
      }
    }


推荐答案

你想加快操作,你可以改变你的代码,所以你根本不做副本。即使用ByteBuffer,或IntBuffer或LongBuffer。这有益于将一份副本保存到堆中的堆已经,但也只有在您使用它时加载。即您的处理可以与加载同时进行。

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.

使用这种方法应该将您的初始加载时间减少到大约10 ms,并且没有保存时间,因为它将已经可用于OS。

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.

这篇关于Java - 存储和上传数组到/从内存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆