我有一个长度为2.2亿(固定)的int和float数组。现在,我想将这些阵列存储到内存和磁盘/从内存和磁盘上载。目前,我正在使用Java
NIO的FileChannel和MappedByteBuffer解决此问题。它可以正常工作,但大约需要5秒钟(Wall Clock
Time)(用于将阵列存储到内存或从内存上载到磁盘或从磁盘上载到磁盘)。实际上,我想要一个更快的。有人可以帮我吗,有没有内置的Java库/数据库/任何其他方法可以更快地上传/存储数组?我特别关心从磁盘上传到内存。我想使其更快。因此,如果存储时间会增加,那我没有问题。提前致谢。
我使用的代码如下(如果需要):
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) ;
}
}