use*_*035 5 java io performance xor
我的Java IO性能有些问题.
首先,我在这里阅读了关于性能的提示,我试图实现它.
但这是我的问题:
使用小文件(高达400MB),速度非常快.但是我将使用的真实文件大约是30 GB.而且这些文件会像地狱一样慢下来.
它的作用:取2个文件,进行独占或写一个新文件.
BTW:不要担心最后的文件被削减.这只是为了解决我还没有找到的一个小错误.
我希望有人给我一个提示.谢谢.
关心蒂莫
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");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
805 次 |
| 最近记录: |