While I was testing my java program, I found out that the first run in a loop takes longer than it does later.
Each of the tasks of the cycle is to make 5 sketches of the same image and save them in a zip file. I am using zip4j and thumbnailator. All 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();
Here is 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 can be seen from the above result, the first run from the cycle takes twice as much time.
What's going on here? And how can I shorten the time for the first launch?
source share