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.