【问题标题】:Java IO Performance XOR with 2 FilesJava IO 性能 XOR 与 2 个文件
【发布时间】:2012-09-21 07:07:42
【问题描述】:

我的 Java IO 性能出现了一些问题。

首先,我在这里阅读了关于性能的提示,并尝试实施。

但这是我的问题:

对于小文件(最大 400MB),速度非常快。 但我将使用的实际文件大约是 30 GB。有了这些,它会像地狱一样慢下来。

它的作用:取 2 个文件,做独占或写一个新文件。

顺便说一句:不要担心文件最后会被剪掉。那只是为了解决我还没有发现的一个小错误。

希望有人给我小费。谢谢。

问候蒂莫

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");

    }
}

【问题讨论】:

  • 如果你有 64 位 JVM,我会使用内存映射文件。如果可以的话,我也会 XOR longs 而不是一次一个字节(这可以快 8 倍)
  • XOR longs 带来了不错的加速,谢谢。 :)

标签: java performance io xor


【解决方案1】:

为什么需要一个 RandomAccessFile 来顺序读取它?

您是否尝试过在 FileInputStream 上使用简单的 BufferedInputStream?

【讨论】:

  • +1 我怀疑他正在使用 RandomAccessFile,因为它用于读写。可以使用 FileInputStream 和 FileOutputStream 代替。
  • 我将实现“文件内”写入或称其为编辑文件。我也尝试过这些......甚至更慢...... :(
猜你喜欢
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 2011-07-08
  • 2021-07-25
  • 1970-01-01
  • 2015-02-16
相关资源
最近更新 更多