Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java IO Performance XOR with 2 Files

I'm having some trouble with my Java IO Performance.

First at all,I've read my tips here about the performance,which i tried to implement.

But here is my Problem:

With small files (up to 400MB), it is pretty fast. But the real files i will be working with are about 30 GB.And with these,it slows down like hell.

What it does: Take 2 Files,do the exclusive or and write a new File.

BTW: Don't worry about the file cut at the end.That is just to fix a little mistake I've not found yet.

I hope someone has a tip for me.Thanks.

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

    }
}
like image 784
user1688035 Avatar asked Nov 04 '22 15:11

user1688035


1 Answers

Why would you need a RandomAccessFile to read it sequentially?

Have you tried using a simple BufferedInputStream over a FileInputStream there?

like image 132
mauhiz Avatar answered Nov 09 '22 13:11

mauhiz