2012-06-22 4 views
2

길이가 2 억 2 천만 (고정) 인 int 및 float 배열이 있습니다. 자, 메모리/디스크에 /에서 배열을 저장/업로드하고 싶습니다. 현재 Java NIO의 FileChannel 및 MappedByteBuffer를 사용하여이 문제를 해결하고 있습니다. 그것은 잘 동작하지만 메모리를 디스크에 저장/업로드하기 위해 약 5 초 (Wall Clock Time)가 소요됩니다. 사실, 나는 더 빠른 것을 원한다. 아무도 나를 도울 수있는 모든 inbuilt 자바 라이브러리/데이터베이스/업로드/저장소를 훨씬 빨리 저장하는 다른 방법은 무엇입니까? 디스크에서 메모리로 업로드하는 데 특별히주의해야합니다. 나는 그것을 더 빨리 만들고 싶다. 그래서, 저장하는데 시간이 걸리면 아무런 문제가 없습니다. 미리 감사드립니다.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) ; 
     } 
    } 
+3

440 만 항목의 경우 5 초가 꽤 좋은 것처럼 들립니다. –

+0

@HunterMcMillen, 나는 더 빨리 원한다. :). – Arpssss

+0

파일이 디스크에 얼마나 큽니까? 나는 헌터와 동의하는 경향이있다. 시간은 나에게 꽤 좋다. 분명히 디스크 성능을 향상시키기 위해 컴퓨터를 업그레이드 할 수 있습니까? SSD가 쓰기에 도움이되는지 확신하지 못합니까? – davidfrancis

답변

3

조작을 가속화하려는 경우, 당신은 당신의 코드를 변경할 수 있습니다. 즉, ByteBuffer 또는 IntBuffer 또는 LongBuffer를 사용하십시오. 이렇게하면 힙을 이미 힙에 저장하는 이점이 있지만 사용하는 동안에 만로드 할 수 있습니다. 즉, 처리가로드와 동시에 수행 될 수 있습니다.

이 접근법을 사용하면 초기 "로드"시간을 약 10 밀리 초로 줄이고 운영 체제에서 이미 사용할 수 있으므로 "저장"시간이 필요하지 않습니다.