Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-存储阵列并将其从内存上传到磁盘_Java_Database - Fatal编程技术网

Java-存储阵列并将其从内存上传到磁盘

Java-存储阵列并将其从内存上传到磁盘,java,database,Java,Database,我有一个长度为2.2亿(固定)的int和float数组。现在,我想在内存和磁盘中存储/上传这些阵列。目前,我正在使用JavaNIO的FileChannel和MappedByteBuffer来解决这个问题。它工作正常,但将阵列存储/上传到内存或从内存上传到磁盘大约需要5秒(挂钟时间)。事实上,我想要更快的。有人能帮我吗?有没有内置的java库/数据库/其他方法可以让上传/存储阵列更快?我特别关心从磁盘上传到内存。我想快点。所以,如果存储时间增加,我没有问题。提前谢谢 我使用的代码如下(如果需要):

我有一个长度为2.2亿(固定)的int和float数组。现在,我想在内存和磁盘中存储/上传这些阵列。目前,我正在使用JavaNIO的FileChannel和MappedByteBuffer来解决这个问题。它工作正常,但将阵列存储/上传到内存或从内存上传到磁盘大约需要5秒(挂钟时间)。事实上,我想要更快的。有人能帮我吗?有没有内置的java库/数据库/其他方法可以让上传/存储阵列更快?我特别关心从磁盘上传到内存。我想快点。所以,如果存储时间增加,我没有问题。提前谢谢

我使用的代码如下(如果需要):

int savenum=220000000;
公共作废保存(){
试一试{
长l=0;
FileChannel通道=新的随机访问文件(str1,“rw”).getChannel();
MappedByteBuffer mbb=channel.map(FileChannel.MapMode.READ_WRITE,0,savenum*8);
顺序(ByteOrder.nativeOrder());
for(int i=0;i
如果您想加快操作速度,您可以更改代码,这样就根本不进行复制。i、 e.使用ByteBuffer、IntBuffer或LongBuffer。这样做的好处是将堆外已经存在的内容保存到堆中,但也只能在使用时加载。i、 e.您的处理可以与加载同时进行


使用这种方法可以将您的初始“加载”时间缩短到10毫秒左右,并且没有“保存”时间,因为操作系统已经可以使用它。

对于4.4亿个项目,5秒听起来很不错。@HunterMcMillen,我希望更快:)。文件在磁盘上有多大?我倾向于同意亨特的观点——时间对我来说相当不错。显然,您可以升级计算机(例如使用RAID)以提高磁盘性能?不确定SSD是否有助于写操作?已经说过,如果您追求读取性能,SSD可能会有所帮助there@davidfrancis谢谢事实上,我正在一台无法升级的服务器上运行它。我只能安装一些软件或运行我的代码。
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) ;
      }
    }