Java performance. How to write a large array to a high-performance disk / SD card?

Is there a way in Java to write a large array of, say, integers to disk? I am doing this on Android and have not found a method that comes close to the very native C code.

The resulting file should not be portable to different machines with different views, so logically just a voluminous record of basic bytes should be sufficient. But I do not know how to do this effectively with Java.

I tried searching the net and tested the following:

  • Serialization is very slow, as expected.
  • Using NIO is still slow - Android tracing shows operations one by one:

Thanks in advance


NIO Code:

int[] array = new array[10000000];

...

raf = new RandomAccessFile(ti.testFileName, "rw");
chan = raf.getChannel();
MappedByteBuffer out = chan.map(FileChannel.MapMode.READ_WRITE, 0, array.length*4);
ib = out.asIntBuffer();
ib.put(array);
out.force();
raf.close();
+3
4

Android, Java IO NIO.

, , , :

byte[] bytes = new byte[10000];
// ...
FileOutputStream out = new FileOutputStream(...);
try {
    out.write(bytes);
} finally {
    out.close();
}

, , . , .

, , - , . , , , , / K.

+1

, , , , . 40 .

NIO 665 62 . , - , .

int[] ints = new int[10 * 1000 * 1000];
long start = System.nanoTime();

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(ints.length*4+4);
byteBuffer.putInt(ints.length);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(ints);
byteBuffer.position(0);

FileChannel fc = new FileOutputStream("main.dat").getChannel();
fc.write(byteBuffer);
fc.force(false);
fc.close();
long time = System.nanoTime() - start;
System.out.println("Write time " + time / 1000 / 1000 + " ms.");

long start2 = System.nanoTime();
FileChannel fc2 = new FileInputStream("main.dat").getChannel();
ByteBuffer lengthBuffer = ByteBuffer.allocate(4);
while(lengthBuffer.remaining()>0) fc2.read(lengthBuffer);
int length = lengthBuffer.getInt(0);

int[] ints2 = new int[length];
ByteBuffer buffer2 = ByteBuffer.allocateDirect(length*4);
while(buffer2.remaining()>0 && fc2.read(buffer2) > 0);
buffer2.flip();
buffer2.asIntBuffer().get(ints2);
long time2 = System.nanoTime() - start2;
System.out.println("Read time " + time2 / 1000 / 1000 + " ms.");

, . BTW: , .

+3

- , , . 89 40 , 500 / ( ). . 40 . , , , . , . , , , .

.

0

0

Source: https://habr.com/ru/post/1781136/


All Articles