2012-09-21 21 views
5

Tengo algunos problemas con mi Java IO Performance.Java IO Performance XOR con 2 archivos

En primer lugar, he leído mis consejos aquí sobre el rendimiento, que traté de implementar.

Pero aquí es mi problema:

Con los archivos pequeños (hasta 400 MB), es bastante rápido. Pero los archivos reales con los que trabajaré son de aproximadamente 30 GB. Y con estos, se ralentiza muchísimo.

Qué hace: Tome 2 archivos, haga el exclusivo o y escriba un nuevo archivo.

BTW: No se preocupe por el corte del archivo al final.Esto es solo para corregir un pequeño error que no he encontrado todavía.

Espero que alguien tenga un consejo para mí.Gracias.

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

    } 
} 
+3

Si tiene una JVM de 64 bits, utilizaría archivos mapeados en la memoria. También podría XOR anhela en lugar de un byte a la vez si puedes (esto puede ser hasta 8 veces más rápido) –

+1

XOR longs trajo una buena velocidad, gracias. :) – user1688035

Respuesta

2

¿Por qué necesitaría un RandomAccessFile para leer de forma secuencial?

¿Has probado usar un BufferedInputStream simple sobre un FileInputStream allí?

+1

+1 Sospecho que está usando RandomAccessFile, ya que se usa para leer y escribir. FileInputStream y FileOutputStream podrían usarse en su lugar. –

+0

Implementaré un archivo "en archivo" o lo llamaré archivo de edición. También intenté estos ... incluso más lento .. :( – user1688035