2
我有一個長度爲2.2億(固定)的int和float數組。現在,我想要將這些陣列存儲/上傳到內存和磁盤。目前,我正在使用Java NIO的FileChannel和MappedByteBuffer來解決這個問題。它工作正常,但需要將近5秒鐘(掛鐘時間)用於將數組存儲/上載到內存或從磁盤上傳數組。其實,我想要更快一點。任何人都可以幫助我,是否有任何內置的java庫/數據庫/任何其他方法來更快地上傳/存儲陣列?我特別關心從磁盤上傳到內存。我想讓它更快。所以,如果存儲時間會增加,我沒有問題。提前致謝。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) ;
}
}
對於4.4億件物品,5秒聽起來不錯。 –
@HunterMcMillen,我想要更快:)。 – Arpssss
文件在磁盤上有多大?我傾向於同意亨特 - 時間對我來說聽起來相當不錯。顯然你可以升級你的電腦(例如用RAID)來提高磁盤性能?不確定SSD有助於寫入? – davidfrancis