4

While I was testing my java program, I found out that the first run in a loop takes more time that the later runs.

Each of loop's task is to make 5 thumbnails of same image and store them in a zip file. I am using zip4j and thumbnailator. All of the runs have the same code.

public static void main(String[] args) throws IOException {
    try {
        for(int i=0;i<10;i++){
            long start = System.currentTimeMillis();
            ZipFile zipFile =  new ZipFile(System.nanoTime()+".zip");
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
            parameters.setIncludeRootFolder(false);
            ArrayList<File> files = new ArrayList<File>();
            for(int j=1;j<5;j++){
                files.add(new File("C:\\savedFile2\\1.jpg"));
            }
            zipFile.createZipFile(files, parameters);


            File zippedFile = zipFile.getFile();
            byte[] buffer = new byte[(int)zippedFile.length()];
            FileInputStream fis = new FileInputStream(zippedFile);
            fis.read(buffer);
            fis.close();
            zippedFile.delete();
            System.out.println("Time taken for "+(i+1)+"th run: "+(System.currentTimeMillis() - start));
        }
    } catch (ZipException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}

Here's my code.

Time taken for 1th run: 58
Time taken for 2th run: 24
Time taken for 3th run: 24
Time taken for 4th run: 24
Time taken for 5th run: 25
Time taken for 6th run: 24
Time taken for 7th run: 25
Time taken for 8th run: 25
Time taken for 9th run: 25
Time taken for 10th run: 29

As you can see from the result above, the first run from the loop takes twice the time as the rest.

What is happening here? And how can I reduce the time for the first run?

Jee Seok Yoon
  • 4,716
  • 9
  • 32
  • 47
  • 7
    This may be due to the overheads that JVM takes before starting your program – sanbhat Aug 29 '13 at 04:59
  • 6
    This is because of [warmup](http://stackoverflow.com/questions/1481853/technique-or-utility-to-minimize-java-warm-up-time) – rocketboy Aug 29 '13 at 04:59
  • It is because the classes has to be loaded. Try the `verbose` option of the command line java-vm. – Claude Aug 29 '13 at 05:29
  • Could it also be that for the first time, the file `"C:\savedFile2\1.jpg"` need to be loaded from the disk while for next iterations, it is already present in the memory or cache. – akaHuman Aug 29 '13 at 05:33
  • I agree that it is certainly because of the overhead of the JVM before starting your program, but you might as well take the parameter initialization out of the loop since it always does the same thing even though it is certainly negligible. – lc2817 Aug 29 '13 at 06:13

1 Answers1

1

I don't think this is related to looping, rather it is related to the createZipFile() function that seems to do some initializing/loading that runs at first time it is called. Consider the following modified example that is producing identical run times in the loop:

   public static void main(String[] args) throws IOException {
    try {
        long _start = System.currentTimeMillis();
        ZipFile _zipFile =  new ZipFile(System.nanoTime()+".zip");
        ZipParameters _parameters = new ZipParameters();
        _parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
        _parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
        _parameters.setIncludeRootFolder(false);
        ArrayList<File> _files = new ArrayList<File>();
        for(int j=1;j<5;j++){
            _files.add(new File("1.jpg"));
        }            
          System.out.println("Initializing files: "+(System.currentTimeMillis() - _start));
        _zipFile.createZipFile(_files, _parameters);
           System.out.println("Initial run: "+(System.currentTimeMillis() - _start));
        for(int i=0;i<10;i++){
            long start = System.currentTimeMillis();
            ZipFile zipFile =  new ZipFile(System.nanoTime()+".zip");
            ZipParameters parameters = new ZipParameters();
            parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
            parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
            parameters.setIncludeRootFolder(false);
            ArrayList<File> files = new ArrayList<File>();
            for(int j=1;j<5;j++){
                files.add(new File("1.jpg"));
            }
            zipFile.createZipFile(files, parameters);


            File zippedFile = zipFile.getFile();
            byte[] buffer = new byte[(int)zippedFile.length()];
            FileInputStream fis = new FileInputStream(zippedFile);
            fis.read(buffer);
            fis.close();
            zippedFile.delete();
               System.out.println("Time taken for "+(i+1)+"tenter code hereh run: "+(System.currentTimeMillis() - start));
        }
    } catch (ZipException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}
KocsisLaci
  • 59
  • 3
  • Seems like your answer is right. As commenters said above, it seems like a additional time taken from warmup and JVM overheads. – Jee Seok Yoon Sep 11 '13 at 01:17