1

I have a simple compressor which converts a file to .zip . How can I find out the compression speed in order to print the speed ?

This is my compressor:

public class Compressor {
    private static byte[] buffer = new byte[1024];

    public static void compress(FileInputStream file) throws IOException {
        FileOutputStream fos = new FileOutputStream("compressedFile.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry zipEntry = new ZipEntry("file.txt");

        zos.putNextEntry(zipEntry);

        int len;
        while ((len = file.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }

        file.close();
        zos.closeEntry();
        zos.close();

        System.out.println("Done");
    }
}
Gustavo
  • 3,461
  • 7
  • 26
  • 41
  • How do you define speed? Get the time the operation requires and divide whatever metric you think is appropriate for an average rate. – duffymo May 23 '16 at 12:17

2 Answers2

3
long startTime = System.nanoTime(); 

compress(); 

long endTime = System.nanoTime(); 
long duration = (endTime - startTime); //divide by 1000000 to get milliseconds
layonez
  • 1,746
  • 1
  • 16
  • 20
  • not all OS support accurate timings down to nano – Lee May 23 '16 at 12:19
  • 1
    @Lee lets say that `nanoTime` is giving you not less accuracy than `currentTimeMillis` but real precision of it is not a nano and depends on target OS – layonez May 23 '16 at 12:25
1
long start = System.currentTimeMillis();
    // stuff you want to time
System.println("Time take: " + ((System.currentTimeMillis() - start) / 1000) + "s");
Lee
  • 738
  • 3
  • 13