我在处理Java IO性能方面遇到了一些问题。
首先,我已经阅读了这里关于性能的一些提示,并尝试着去实现它们。
但是,这里是我的问题:
当处理小文件(最多400MB)时,速度很快。 但是,我要处理的真实文件大小大约为30GB。对于这些文件,处理速度慢得像鬼一样。
它所做的事情是:取两个文件,执行异或操作并写入新文件。
顺便说一句:不用担心在结尾处的文件切割。那只是为了纠正我还没有发现的一个小错误。
我希望有人可以给我一些提示。谢谢。
祝好, Timo
首先,我已经阅读了这里关于性能的一些提示,并尝试着去实现它们。
但是,这里是我的问题:
当处理小文件(最多400MB)时,速度很快。 但是,我要处理的真实文件大小大约为30GB。对于这些文件,处理速度慢得像鬼一样。
它所做的事情是:取两个文件,执行异或操作并写入新文件。
顺便说一句:不用担心在结尾处的文件切割。那只是为了纠正我还没有发现的一个小错误。
我希望有人可以给我一些提示。谢谢。
祝好, Timo
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class main {
public static void main(String[] args) throws IOException {
final long start = System.currentTimeMillis();
// Init. FileChannel1
final File file1 = new File("/home/tmann/Downloads/test/1.zip");
final RandomAccessFile fis1 = new RandomAccessFile(file1, "rw");
final FileChannel ch1 = fis1.getChannel();
// Init. FileChannel2
final File file2 = new File("/home/tmann/Downloads/test/2.zip");
final RandomAccessFile fis2 = new RandomAccessFile(file2, "rw");
final FileChannel ch2 = fis2.getChannel();
// Init FileChannel3
final File file3 = new File("/home/tmann/Downloads/test/32.zip");
final RandomAccessFile fis3 = new RandomAccessFile(file3, "rw");
final FileChannel ch3 = fis3.getChannel();
// Init ByteBuffer1
final ByteBuffer bytebuffer1 = ByteBuffer.allocate(65536);
// Init ByteBuffer2
final ByteBuffer bytebuffer2 = ByteBuffer.allocate(65536);
// Init ByteBuffer3
final ByteBuffer bytebuffer3 = ByteBuffer.allocate(65536);
int byte1 = 0;
int byte2 = 0;
final byte[] array1 = bytebuffer1.array();
final byte[] array2 = bytebuffer2.array();
final byte[] array3 = bytebuffer3.array();
int count = 0;
while (byte1 != -1) {
byte1=ch1.read(bytebuffer1);
byte2=ch2.read(bytebuffer2);
while (count < byte1) {
array3[count] = (byte) (array1[count] ^ array2[count]);
count++;
}
bytebuffer3.put(array3);
bytebuffer1.flip();
bytebuffer2.flip();
bytebuffer3.flip();
while (bytebuffer3.hasRemaining()) {
ch3.write(bytebuffer3);
}
bytebuffer1.clear();
bytebuffer2.clear();
bytebuffer3.clear();
}
fis3.setLength(fis3.length()-59858);
final long endvar = System.currentTimeMillis();
System.out.println((500.0 / ((endvar - start) / 1000f)) + "MB/s");
}
}